@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
32 lines (28 loc) • 927 B
JavaScript
'use client';
;
var React = require('react');
function useDebouncedState(defaultValue, wait, options = { leading: false }) {
const [value, setValue] = React.useState(defaultValue);
const timeoutRef = React.useRef(null);
const leadingRef = React.useRef(true);
const clearTimeout = () => window.clearTimeout(timeoutRef.current);
React.useEffect(() => clearTimeout, []);
const debouncedSetValue = React.useCallback(
(newValue) => {
clearTimeout();
if (leadingRef.current && options.leading) {
setValue(newValue);
} else {
timeoutRef.current = window.setTimeout(() => {
leadingRef.current = true;
setValue(newValue);
}, wait);
}
leadingRef.current = false;
},
[options.leading]
);
return [value, debouncedSetValue];
}
exports.useDebouncedState = useDebouncedState;
//# sourceMappingURL=use-debounced-state.cjs.map