Update hooks/useDebounce.ts

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
Mr. Algorithm 2025-10-04 19:49:40 +05:30 committed by GitHub
parent c1bc573dcb
commit 502b52e4f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 3 deletions

View File

@ -4,12 +4,16 @@ import { useCallback, useRef } from 'react';
export function useDebounce(callback: () => void, delay: number) {
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const callbackRef = useRef(callback);
// Keep callback ref up to date
callbackRef.current = callback;
return useCallback(() => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(callback, delay);
}, [callback, delay])
timeoutRef.current = setTimeout(() => callbackRef.current(), delay);
}, [delay])
}