|
Download react-use/docs/useEvent.md from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 677 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-use/docs/useEvent.md
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/react-use/docs/useEvent.md
-
curl -L -o useEvent.md https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-use/docs/useEvent.md
677 Bytes
useEvent
React sensor hook that subscribes a handler to events.
Usage
import {useEvent, useList} from 'react-use';
const Demo = () => {
const [list, {push, clear}] = useList();
const onKeyDown = useCallback(({key}) => {
if (key === 'r') clear();
push(key);
}, []);
useEvent('keydown', onKeyDown);
return (
<div>
<p>
Press some keys on your keyboard, <code style={{color: 'tomato'}}>r</code> key resets the list
</p>
<pre>
{JSON.stringify(list, null, 4)}
</pre>
</div>
);
};
Examples
useEvent('keydown', handler)
useEvent('scroll', handler, window, {capture: true})