Skip to content

Latest commit

 

History

History
34 lines (26 loc) · 566 Bytes

useClickAway.md

File metadata and controls

34 lines (26 loc) · 566 Bytes

useClickAway

React UI hook that triggers a callback when user clicks outside the target element.

Usage

import {useClickAway} from 'react-use';

const Demo = () => {
  const ref = useRef(null);
  useClickAway(ref, () => {
    console.log('OUTSIDE CLICKED');
  });

  return (
    <div ref={ref} style={{
      width: 200,
      height: 200,
      background: 'red',
    }} />
  );
};

Reference

useClickAway(ref, onMouseEvent)
useClickAway(ref, onMouseEvent, ['click'])
useClickAway(ref, onMouseEvent, ['mousedown', 'touchstart'])