@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
31 lines (30 loc) • 950 B
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _debounce = _interopRequireDefault(require("lodash/debounce"));
var _react = require("react");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const useDebounceFn = (fn, options) => {
const fnRef = (0, _react.useRef)(fn);
const optionsRef = (0, _react.useRef)(options);
fnRef.current = fn;
const wait = options?.wait ?? 1000;
const debounced = (0, _react.useMemo)(() => (0, _debounce.default)((...args) => {
// @ts-ignore
return fnRef.current?.(...args);
}, wait, optionsRef.current), [wait]);
(0, _react.useEffect)(() => {
return () => {
debounced.cancel();
};
}, [debounced]);
return {
run: debounced,
cancel: debounced.cancel,
flush: debounced.flush
};
};
var _default = exports.default = useDebounceFn;
//# sourceMappingURL=useDebounceFn.js.map
;