diff --git a/hooks/useDebounce.ts b/hooks/useDebounce.ts index 3a6b138..2ffbe2f 100644 --- a/hooks/useDebounce.ts +++ b/hooks/useDebounce.ts @@ -4,12 +4,16 @@ import { useCallback, useRef } from 'react'; export function useDebounce(callback: () => void, delay: number) { const timeoutRef = useRef(null); + const callbackRef = useRef(callback); + + // Keep callback ref up to date + callbackRef.current = callback; return useCallback(() => { - if(timeoutRef.current) { + if (timeoutRef.current) { clearTimeout(timeoutRef.current); } - timeoutRef.current = setTimeout(callback, delay); - }, [callback, delay]) + timeoutRef.current = setTimeout(() => callbackRef.current(), delay); + }, [delay]) } \ No newline at end of file