react-use/docs/useEvent.md
2019-03-28 19:35:00 +01:00

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})