Created
January 26, 2023 07:23
-
-
Save JoueBien/68a2304476008daffcd6162026d34a9d to your computer and use it in GitHub Desktop.
useKeyUp
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"; | |
export function useKeyUp(callback: (event: KeyboardEvent) => void) { | |
function onKeyPress(event: KeyboardEvent) { | |
callback(event); | |
} | |
// On Mount and callback change | |
useEffect(() => { | |
document.addEventListener("keyup", onKeyPress); | |
// Clean up | |
return () => { | |
document.removeEventListener("keyup", onKeyPress); | |
}; | |
}, [callback]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment