@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
23 lines (22 loc) • 895 B
JavaScript
"use client";
let react = require("react");
//#region packages/@mantine/hooks/src/use-debounced-state/use-debounced-state.ts
function useDebouncedState(defaultValue, wait, options = { leading: false }) {
const [value, setValue] = (0, react.useState)(defaultValue);
const timeoutRef = (0, react.useRef)(null);
const leadingRef = (0, react.useRef)(true);
const clearTimeout = () => window.clearTimeout(timeoutRef.current);
(0, react.useEffect)(() => clearTimeout, []);
return [value, (0, 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])];
}
//#endregion
exports.useDebouncedState = useDebouncedState;
//# sourceMappingURL=use-debounced-state.cjs.map