Created
November 15, 2018 03:13
-
-
Save cigrainger/5e5e19382ddbefc24d8fb69f0d155b64 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 useOnClickOutside = (ref, handler) => { | |
useEffect(() => { | |
const listener = (event) => { | |
if (!ref.current || ref.current.contains(event.target)) { | |
return; | |
} | |
handler(event); | |
}; | |
document.addEventListener("mousedown", listener); | |
return () => { | |
document.removeEventListener("mousedown", listener); | |
}; | |
}, []); | |
}; | |
export default useOnClickOutside; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment