Skip to content

Commit 6a29fc6

Browse files
cikzhpraseodym
andauthored
Wrapped the callback in a ref
Co-authored-by: Mark Janssen <[email protected]>
1 parent 1bdb849 commit 6a29fc6

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

frontend/lib/util/hook/useDebouncedCallback.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ export function useDebouncedCallback<T extends (...args: never[]) => unknown>(
44
callback: T,
55
delay: number,
66
) {
7+
const callbackRef = useRef(callback);
78
const debounceTimerRef = useRef(0);
9+
810
useEffect(() => {
911
return () => {
1012
window.clearTimeout(debounceTimerRef.current);
@@ -14,9 +16,8 @@ export function useDebouncedCallback<T extends (...args: never[]) => unknown>(
1416
return useCallback(
1517
(...args: Parameters<T>) => {
1618
window.clearTimeout(debounceTimerRef.current);
17-
debounceTimerRef.current = window.setTimeout(() => callback(...args), delay);
19+
debounceTimerRef.current = window.setTimeout(() => callbackRef.current(...args), delay);
1820
},
19-
// eslint-disable-next-line react-hooks/exhaustive-deps
2021
[delay],
2122
);
2223
}

0 commit comments

Comments
 (0)