Skip to content

Instantly share code, notes, and snippets.

@kevinfelisilda
Created March 31, 2019 07:06
Show Gist options
  • Save kevinfelisilda/30d215d0cd4743d5b7949c0a287d62f5 to your computer and use it in GitHub Desktop.
Save kevinfelisilda/30d215d0cd4743d5b7949c0a287d62f5 to your computer and use it in GitHub Desktop.
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