Created
March 31, 2019 07:06
-
-
Save kevinfelisilda/30d215d0cd4743d5b7949c0a287d62f5 to your computer and use it in GitHub Desktop.
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
import { useEffect } from "react"; | |
const useOutsideClick = (ref, callback) => { | |
const handleClick = e => { | |
if (ref.current && !ref.current.contains(e.target)) { | |
callback(); | |
} | |
}; | |
useEffect(() => { | |
document.addEventListener("click", handleClick); | |
return () => { | |
document.removeEventListener("click", handleClick); | |
}; | |
}); | |
}; | |
export default useOutsideClick; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment