Last active
January 29, 2019 11:44
-
-
Save HaNdTriX/6ef6f62eadfc87767e92e900070f620a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from 'react' | |
function useClickOutside(ref, onClickOutside) { | |
useEffect(() => { | |
const handleClick = (event) => { | |
if (!ref.current || !ref.current.contains(event.target)) { | |
onClickOutside(event) | |
} | |
} | |
window.addEventListener('click', handleClick) | |
return () => window.removeEventListener('click', handleClick) | |
}, [onClickOutside]) | |
} | |
export default useClickOutside |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage