Created
January 9, 2026 13:22
-
-
Save MarekZeman91/d86d96e263cfb12735747bb6b8549a15 to your computer and use it in GitHub Desktop.
This file contains hidden or 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' | |
| import { useFunction } from './useFunction' | |
| /** | |
| * Hook to use native event listener | |
| * @param eventName | |
| * @param listener Does not have to be memorized | |
| */ | |
| export function useNativeEvent<K extends keyof GlobalEventHandlersEventMap>( | |
| eventName: K, | |
| listener: (event: GlobalEventHandlersEventMap[K]) => void | |
| ) { | |
| const callback = useFunction(listener) | |
| useEffect(() => { | |
| window.addEventListener(eventName, callback) | |
| return () => window.removeEventListener(eventName, callback) | |
| }, [eventName, callback]) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment