I have a custom event listener hook that looks like this:
const useEventListener = (
eventName: string,
handler: ({key} : KeyboardEvent) => void,
) => {
const savedHandler = useRef<({key} : KeyboardEvent) => void>(handler)
useEffect(() => {
savedHandler.current = handler
}, [handler])
useEffect(() => {
const eventListener = (event: KeyboardEvent) => savedHandler.current(event)
window.addEventListener(eventName, eventListener);
return () => {
window.removeEventListener(eventName, eventListener)
}
}, [eventName])
}
This is throwing the error,
TS error: Type 'event' is missing the following properties from type 'keyboardevent': char, key and 18 others
Am I typing the useRef incorrectly? I wasn't able to find anything from here that work either. Not sure what I'm doing wrong here.