import { useLayoutEffect, useRef, useCallback, useEffect } from 'react'; export const useDebounce = void>( callback: T, delay: number, ) => { const callbackRef = useRef(callback); const timeoutRef = useRef(null); useLayoutEffect(() => { callbackRef.current = callback; }); useEffect(() => { return () => { if (timeoutRef.current) { clearTimeout(timeoutRef.current); } }; }, []); return useCallback( (...args: Parameters) => { if (timeoutRef.current) { clearTimeout(timeoutRef.current); } timeoutRef.current = setTimeout(() => { callbackRef.current(...args); }, delay); }, [delay], ); };