export function useDebounce(fn:()=>any, delay:Number, dep = []) {
const { current } = useRef({ fn, timer: null });
console.log('useDebounce coming')
useEffect(function () {
current.fn = fn;
}, [fn]);
return useCallback(function f(...args) {
if (current.timer) {
clearTimeout(current.timer);
current.delay = delay
}
current.timer = setTimeout(() => {
current.fn.call(this, ...args);
}, current.delay||0);
}, dep)
}
React 防抖 hooks useDebounce
最新推荐文章于 2024-08-24 08:23:46 发布