Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Created November 15, 2021 07:16
Show Gist options
  • Save Lightnet/2ca07735d65d4098a0566d2e14cc0fa1 to your computer and use it in GitHub Desktop.
Save Lightnet/2ca07735d65d4098a0566d2e14cc0fa1 to your computer and use it in GitHub Desktop.
found simple useEvent for react
//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