UNPKG

@rooks/use-debounce

Version:
29 lines (26 loc) 898 B
import { useRef, useEffect } from 'react'; import debounce from 'lodash.debounce'; /** * Debounce hook * @param {function} callback The callback to debounce * @param {number} wait The duration to debounce * @returns {function} The debounced callback */ function useDebounce(callback, wait, options) { function createDebouncedCallback(fn) { return debounce(fn, wait, options); } const callbackRef = useRef(callback); const debouncedCallbackRef = useRef(createDebouncedCallback(callback)); useEffect(() => { callbackRef.current = callback; }); useEffect(() => { debouncedCallbackRef.current = createDebouncedCallback((...args) => { callbackRef.current(...args); }); }, [wait, options]); return debouncedCallbackRef.current; } export default useDebounce; //# sourceMappingURL=index.esm.js.map