@totalsoft/rocket-ui
Version:
A set of reusable and composable React components built on top of Material UI core for developing fast and friendly web applications interfaces.
18 lines • 764 B
JavaScript
import debounce from 'lodash/debounce';
import { useMemo, useRef } from 'react';
const useDebouncedCallback = (callback, debounceBy) => {
const debouncedCallbackRef = useRef(undefined);
const debouncedCallback = useMemo(() => {
if (debounceBy) {
if (debouncedCallbackRef?.current?.cancel && typeof debouncedCallbackRef.current.cancel === 'function')
debouncedCallbackRef.current.cancel();
const debounced = debounce(callback, debounceBy);
debouncedCallbackRef.current = debounced;
return debounced;
}
return callback;
}, [callback, debounceBy]);
return debouncedCallback;
};
export default useDebouncedCallback;
//# sourceMappingURL=useDebouncedCallback.js.map