@rooks/use-debounce
Version:
Debounce hook for react
29 lines (26 loc) • 898 B
JavaScript
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