Created
December 1, 2022 13:40
-
-
Save Iamsheye/e9590369b7f6928e673730503bb04124 to your computer and use it in GitHub Desktop.
Outside Click Handler
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
const emojiRef = useRef<HTMLDivElement>(null); | |
useEffect(() => { | |
const handleClickOutside = (event: MouseEvent) => { | |
if (emojiRef.current && !emojiRef.current.contains(event.target as Node)) { | |
// write your outside click function | |
} | |
}; | |
document.addEventListener('click', handleClickOutside, true); | |
return () => { | |
document.removeEventListener('click', handleClickOutside, true); | |
}; | |
}, []); | |
// Attach "emojiRef" to an element |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment