UNPKG

use-throttled-effect

Version:
23 lines (19 loc) 531 B
import { useEffect, useRef } from 'react'; export const useThrottledEffect = (callback, delay, deps = []) => { const lastRan = useRef(Date.now()); useEffect( () => { const handler = setTimeout(function() { if (Date.now() - lastRan.current >= delay) { callback(); lastRan.current = Date.now(); } }, delay - (Date.now() - lastRan.current)); return () => { clearTimeout(handler); }; }, [delay, ...deps], ); }; export default useThrottledEffect;