Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { 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;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement