Skip to content

Instantly share code, notes, and snippets.

@JoueBien
Created January 26, 2023 07:23
Show Gist options
  • Save JoueBien/68a2304476008daffcd6162026d34a9d to your computer and use it in GitHub Desktop.
Save JoueBien/68a2304476008daffcd6162026d34a9d to your computer and use it in GitHub Desktop.
useKeyUp
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