import { useEffect, useRef, useState } from 'react'; export const useThrottle = (value: T, ms: number = 200) => { const [state, setState] = useState(value); const timeout = useRef>(); const nextValue = useRef(null) as any; const hasNextValue = useRef(0) as any; const fnRef = useRef(null) as any; useEffect(() => { if (!timeout.current) { setState(value); const timeoutCallback = () => { if (hasNextValue.current) { hasNextValue.current = false; setState(nextValue.current); timeout.current = setTimeout(timeoutCallback, ms); } else { timeout.current = undefined; } }; timeout.current = setTimeout(timeoutCallback, ms); } else { nextValue.current = value; hasNextValue.current = true; } }, [value]); const fn = () => { timeout.current && clearTimeout(timeout.current); }; fnRef.current = fn; useEffect(() => () => fnRef.current(), []); return state; };