UNPKG

@nikitababko/react-custom-hooks

Version:
20 lines (16 loc) 410 B
import { useCallback, useRef } from 'react'; export default function useDebounce(callback, delay) { const timer = useRef(); const debouncedCallback = useCallback( (...args) => { if (timer.current) { clearTimeout(timer.current); } timer.current = setTimeout(() => { callback(...args); }, delay); }, [callback, delay] ); return debouncedCallback; }