Created
November 15, 2021 07:16
-
-
Save Lightnet/2ca07735d65d4098a0566d2e14cc0fa1 to your computer and use it in GitHub Desktop.
found simple useEvent for react
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
//https://atomizedobjects.com/blog/javascript/develop-2d-javascript-games-html5-react/ | |
import { useEffect } from 'react'; | |
export default function useEvent(event, handler) { | |
useEffect(() => { | |
// initiate the event handler | |
window.addEventListener(event, handler); | |
// this will clean up the event every time the component is re-rendered | |
return function cleanup() { | |
window.removeEventListener(event, handler); | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment