Created
October 27, 2022 19:05
-
-
Save itispx/b9466e3cce8263a49e5b02a62e81b9be to your computer and use it in GitHub Desktop.
useKeydown hook
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 useKeydown = (func, dependencies) => { | |
useEffect(() => { | |
document.addEventListener("keydown", func, false); | |
return () => { | |
document.removeEventListener("keydown", func, false); | |
}; | |
}, [func, dependencies]); | |
}; | |
export default useKeydown; |
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 useKeydown = ( | |
func: (event: KeyboardEvent) => void, | |
dependencies: [] | undefined, | |
) => { | |
useEffect(() => { | |
document.addEventListener("keydown", func, false); | |
return () => { | |
document.removeEventListener("keydown", func, false); | |
}; | |
}, [func, dependencies]); | |
}; | |
export default useKeydown; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment