useEvent

React传感器钩子,它为事件订阅handler

Usage

  1. import useEvent from 'react-use/lib/useEvent';
  2. import useList from 'react-use/lib/useList';
  3. const Demo = () => {
  4. const [list, {push, clear}] = useList();
  5. const onKeyDown = useCallback(({key}) => {
  6. if (key === 'r') clear();
  7. push(key);
  8. }, []);
  9. useEvent('keydown', onKeyDown);
  10. return (
  11. <div>
  12. <p>
  13. Press some keys on your keyboard, <code style={{color: 'tomato'}}>r</code> key resets the list
  14. </p>
  15. <pre>
  16. {JSON.stringify(list, null, 4)}
  17. </pre>
  18. </div>
  19. );
  20. };

Examples

  1. useEvent('keydown', handler)
  2. useEvent('scroll', handler, window, {capture: true})