feat: search test case (#277)

This commit is contained in:
Eliezer Castro
2025-08-30 11:26:46 -03:00
committed by GitHub
parent afe569a79c
commit f76a3cdaf5
12 changed files with 266 additions and 102 deletions

View File

@@ -0,0 +1,30 @@
import { useRef, useCallback, useEffect } from 'react';
export default function useDebounce<T extends (...args: any[]) => void>(
fn: T,
delay: number
): (...args: Parameters<T>) => void {
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const debounceFn = useCallback(
(...args: Parameters<T>): void => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(() => {
fn(...args);
}, delay);
},
[fn, delay]
);
useEffect(() => {
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, []);
return debounceFn;
}