UNPKG

@td-design/rn-hooks

Version:

从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率

41 lines (38 loc) 1.33 kB
import { __spreadArray, __read } from '../_virtual/_tslib.js'; import { throttle } 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 useThrottleFn(fn, options) { var _a; if (__DEV__) { if (typeof fn !== 'function') { throw new Error("useThrottleFn 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 throttled = useCreation(function () { return throttle(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 () { throttled.cancel(); }); return { run: throttled, cancel: throttled.cancel, flush: throttled.flush, }; } export { useThrottleFn as default };