@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
45 lines (36 loc) • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _debounce = _interopRequireDefault(require("lodash/debounce"));
var _react = require("react");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const useDebounceFn = (fn, options) => {
var _options$wait;
const fnRef = (0, _react.useRef)(fn);
const optionsRef = (0, _react.useRef)(options);
fnRef.current = fn;
const wait = (_options$wait = options === null || options === void 0 ? void 0 : options.wait) !== null && _options$wait !== void 0 ? _options$wait : 1000;
const debounced = (0, _react.useMemo)(() => (0, _debounce.default)(function () {
var _fnRef$current;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
// @ts-ignore
return (_fnRef$current = fnRef.current) === null || _fnRef$current === void 0 ? void 0 : _fnRef$current.call(fnRef, ...args);
}, wait, optionsRef.current), [wait]);
(0, _react.useEffect)(() => {
return () => {
debounced.cancel();
};
}, [debounced]);
return {
run: debounced,
cancel: debounced.cancel,
flush: debounced.flush
};
};
var _default = useDebounceFn;
exports.default = _default;
//# sourceMappingURL=useDebounceFn.js.map