@toolpad/utils
Version:
Shared utilities used by Toolpad packages.
29 lines (28 loc) • 927 B
JavaScript
import * as React from 'react';
/**
* This hook allows you to debounce any fast changing value. The debounced value will only
* reflect the latest value when the useDebounce hook has not been called for the specified
* time period.
*
* Inspired by https://usehooks.com/useDebounce/
*/
export default function useDebounced(value, delay) {
const [debouncedValue, setDebouncedValue] = React.useState(() => value);
const timeoutIdRef = React.useRef(null);
React.useEffect(() => () => {
if (timeoutIdRef.current) {
clearTimeout(timeoutIdRef.current);
timeoutIdRef.current = null;
}
}, []);
React.useEffect(() => {
timeoutIdRef.current = setTimeout(() => setDebouncedValue(() => value), delay);
return () => {
if (timeoutIdRef.current) {
clearTimeout(timeoutIdRef.current);
timeoutIdRef.current = null;
}
};
}, [value, delay]);
return debouncedValue;
}