@td-design/rn-hooks
Version:
从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率
41 lines (38 loc) • 1.33 kB
JavaScript
import { __spreadArray, __read } from '../_virtual/_tslib.js';
import { debounce } from 'lodash-es';
import useCreation from '../useCreation/index.js';
import useMemoizedFn from '../useMemoizedFn/index.js';
import useUnmount from '../useUnmount/index.js';
/**
* 用来处理防抖函数的 Hook。
* @param fn 需要防抖的函数
* @param options 配置防抖的行为
*/
function useDebounceFn(fn, options) {
var _a;
if (__DEV__) {
if (typeof fn !== 'function') {
throw new Error("useDebounceFn expected parameter is a function, got ".concat(typeof fn));
}
}
var fnRef = useMemoizedFn(fn);
var wait = (_a = options === null || options === void 0 ? void 0 : options.wait) !== null && _a !== void 0 ? _a : 1000;
var debounced = useCreation(function () {
return debounce(function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return fnRef.apply(void 0, __spreadArray([], __read(args), false));
}, wait, options);
}, []);
useUnmount(function () {
debounced.cancel();
});
return {
run: debounced,
cancel: debounced.cancel,
flush: debounced.flush,
};
}
export { useDebounceFn as default };