useOutsideClick

React 用户界面钩子,当用户单击目标元素外部时触发回调。

用法

  1. import {useOutsideClick} from 'react-use';
  2. const Demo = () => {
  3. const ref = useRef(null);
  4. useOutsideClick(ref, () => {
  5. console.log('OUTSIDE CLICKED');
  6. });
  7. return (
  8. <div ref={ref} style={{
  9. width: 200,
  10. height: 200,
  11. background: 'red',
  12. }} />
  13. );
  14. };