mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
624 B
624 B
useEvent
React sensor hook that subscribes a handler to events.
Usage
import useEvent from 'react-use/lib/useEvent';
import useList from 'react-use/lib/useList';
const Demo = () => {
const [list, {push}] = useList();
const onkeydown = ({key}) => {
push(key);
};
useEvent('keydown', useCallback(onkeydown, []));
return (
<div>
<p>
Press some keys on your keyboard.
</p>
<pre>
{JSON.stringify(list, null, 4)}
</pre>
</div>
);
};
Examples
useEvent('keydown', handler)
useEvent('scroll', handler, window, {capture: true})