Skip to content

Latest commit

 

History

History
46 lines (31 loc) · 722 Bytes

useKey.md

File metadata and controls

46 lines (31 loc) · 722 Bytes

useKey

当使用键盘键时,React UI 传感器钩子执行handler

Usage

import useKey from 'react-use/lib/useKey';

const Demo = () => {
  const [count, set] = useState(0);
  const increment = () => set(count => ++count);
  useKey('ArrowUp', increment);

  return (
    <div>
      Press arrow up: {count}
    </div>
  );
};

Or as render-prop:

import UseKey from 'react-use/lib/comps/UseKey';

<UseKey filter='a' fn={() => alert('"a" key pressed!')} />

Reference

useKey(filter, handler, options?, deps?)

Examples

useKey('a', () => alert('"a" pressed'));

const predicate = (event) => event.key === 'a'
useKey(predicate, handler, {event: 'keyup'});