Created
April 21, 2020 22:41
-
-
Save worc/799c7d20189cecd715b2183d504751c6 to your computer and use it in GitHub Desktop.
ok, so i've definitely done custom hooks with a useState before
This file contains 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
function clickAwayListener (elementId, callback) { | |
return event => { | |
const outsideClick = !document.getElementById(elementId).contains(event.target) | |
if (outsideClick) { | |
callback() | |
} | |
} | |
} | |
export function useClickAway (elementId, callback) { | |
const listener = clickAwayListener(elementId, callback) | |
useEffect(() => { | |
document.addEventListener('click', listener) | |
return () => document.removeEventListener('click', listener) | |
}, []) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment