use-throttled-effect
Version:
Throttled effect hook for react
23 lines (19 loc) • 531 B
JavaScript
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;