React hook for window keyup using use-event-target.
npm install use-keyupGet window size by hooks keyup event without side-effect.
``sh`
$ npm install use-keyup
This example will auto removeEventListener when component unmounted.
` keyup {key}javascript`
import useKeyup from 'use-keyup';
const demo = () => {
const [key, setKey] = useState([]);
useKeyup(e => setKey(e.code));
return
};
Explicity Clean the handler.
` keyup {key}javascript`
import useKeyup from 'use-keyup';
const demo = () => {
const [key, setKey] = useState([]);
const [cleanUp] = useKeyup(e => setKey(e.code));
useEffect(() => {
setTimeout(cleanUp, 3000);
// Set timer when mounted & after 3sec clean keyup
}, []);
return
};
Call clean-up function (to removeEventListener) when component has been unmounted.
Also, I export a cleanUp function, you can use this to explicity remove events when you need.
This is a hooks infrastructure for easily package a event hooks.
See more useEventTarget.