20 lines
569 B
JavaScript
20 lines
569 B
JavaScript
|
"use strict";
|
||
|
|
||
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.default = useDebounce;
|
||
|
var React = _interopRequireWildcard(require("react"));
|
||
|
function useDebounce(value) {
|
||
|
const [cacheValue, setCacheValue] = React.useState(value);
|
||
|
React.useEffect(() => {
|
||
|
const timeout = setTimeout(() => {
|
||
|
setCacheValue(value);
|
||
|
}, value.length ? 0 : 10);
|
||
|
return () => {
|
||
|
clearTimeout(timeout);
|
||
|
};
|
||
|
}, [value]);
|
||
|
return cacheValue;
|
||
|
}
|