import { useCallback, useRef, useEffect } from 'react';
function useDebounce(fn, delay, dep = []) {
const { current } = useRef({ fn, timer: null });
useEffect(
function () {
current.fn = fn;
},
[fn],
);
return useCallback(function f(...args) {
if (current.timer) {
clearTimeout(current.timer);
}
current.timer = setTimeout(() => {
current.fn(...args);
}, delay);
}, dep);
}
export default useDebounce;