Merge branch 'master' into fix/read-only
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user