UNPKG

rc-hooks

Version:
38 lines (37 loc) 1.28 kB
import { useRef } from 'react'; import { debounce } from 'ut2'; import useUnmount from '../useUnmount'; import useLatest from '../useLatest'; /** * 用来处理防抖函数的 Hook。 * * @param {Function} fn 需要防抖的函数。 * @param {number} [wait=0] 防抖等待时间,单位为毫秒。默认 `0`。 * @param {boolean} [immediate=false] 是否在延迟开始前调用。默认 `false`。 * @returns * @example * const [value, setValue] = useState(0); * // 频繁调用 run,但只会在所有调用完成 500ms 后执行一次函数 * const { run } = useDebounceFn(setValue, 500); */ function useDebounceFn(fn, wait, immediate) { if (wait === void 0) { wait = 0; } if (immediate === void 0) { immediate = false; } var fnRef = useLatest(fn); var debounceRun = useRef(debounce(function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return fnRef.current.apply(void 0, args); }, wait, immediate)); useUnmount(function () { debounceRun.current.cancel(); }); return { run: debounceRun.current, cancel: debounceRun.current.cancel, flush: debounceRun.current.flush }; } export default useDebounceFn;