Merge branch 'master' into fix/read-only

This commit is contained in:
Jarrod Flesch
2022-09-30 10:47:34 -04:00
7 changed files with 30 additions and 18 deletions

View File

@@ -4,10 +4,10 @@ import { useEffect, useRef } from 'react';
type useThrottledEffect = (callback: React.EffectCallback, delay: number, deps: React.DependencyList) => void;
const useThrottledEffect: useThrottledEffect = (callback, delay, deps = []) => {
const lastRan = useRef<number>(null);
const lastRan = useRef(Date.now());
useEffect(() => {
if (lastRan) {
useEffect(
() => {
const handler = setTimeout(() => {
if (Date.now() - lastRan.current >= delay) {
callback();
@@ -18,12 +18,9 @@ const useThrottledEffect: useThrottledEffect = (callback, delay, deps = []) => {
return () => {
clearTimeout(handler);
};
}
callback();
lastRan.current = Date.now();
return () => null;
}, [delay, ...deps]);
},
[delay, ...deps],
);
};
export default useThrottledEffect;