diff --git a/src/hooks/useClickOutside.ts b/src/hooks/useClickOutside.ts index 531e679..4ce7ea4 100644 --- a/src/hooks/useClickOutside.ts +++ b/src/hooks/useClickOutside.ts @@ -1,22 +1,22 @@ -import { useEffect, useRef } from 'react'; +import { useEffect, useRef, useCallback } from "react"; -export default function useClickOutside( - callback: () => void -) { +export default function useClickOutside(callback: () => void) { const ref = useRef(null); + const memoizedCallback = useCallback(callback, [callback]); + useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (ref.current && !ref.current.contains(event.target as Node)) { - callback(); + memoizedCallback(); } }; - document.addEventListener('mousedown', handleClickOutside); + document.addEventListener("mousedown", handleClickOutside); return () => { - document.removeEventListener('mousedown', handleClickOutside); + document.removeEventListener("mousedown", handleClickOutside); }; - }, []); + }, [memoizedCallback]); return ref; }