react-usehooks-ts
Version:
A collections of typescript supported react Custom hooks
19 lines (18 loc) • 666 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDebounce = void 0;
const react_1 = require("react");
const useDebounce = (value, options = {}) => {
const { delay = 500, minLength } = options;
const [debounceValue, setDebounceValue] = (0, react_1.useState)(null);
(0, react_1.useEffect)(() => {
const timerId = setTimeout(() => {
if (value && value.length > (minLength || 3)) {
setDebounceValue(value);
}
}, delay);
return () => clearTimeout(timerId);
}, [value, delay, minLength]);
return debounceValue;
};
exports.useDebounce = useDebounce;
;