Created
October 29, 2022 15:49
-
-
Save zaknesler/7dbae835b0884b7645135d2477c365d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from 'react' | |
const useEvent = <T extends keyof WindowEventMap>( | |
event: T, | |
handler: (this: Window, ev: WindowEventMap[T]) => any, | |
options?: boolean | AddEventListenerOptions, | |
) => { | |
useEffect(() => { | |
window.addEventListener(event, handler, options) | |
return () => window.removeEventListener(event, handler) | |
}, [event, handler, options]) | |
} | |
export default useEvent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment