@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
34 lines (29 loc) • 1.07 kB
JavaScript
import debounce from 'lodash/debounce';
import { useRef, useMemo, useEffect } from 'react';
const useDebounceFn = (fn, options) => {
var _options$wait;
const fnRef = useRef(fn);
const optionsRef = 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 = useMemo(() => debounce(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]);
useEffect(() => {
return () => {
debounced.cancel();
};
}, [debounced]);
return {
run: debounced,
cancel: debounced.cancel,
flush: debounced.flush
};
};
export default useDebounceFn;
//# sourceMappingURL=useDebounceFn.js.map