UNPKG

@schema-render/core-react

Version:

Through a set of simple JSON Schema, efficiently build a set of forms.

21 lines (20 loc) 758 B
/** * Fork from https://github.com/alibaba/hooks/blob/master/packages/hooks/src/useDebounceFn/index.ts */ import { useMemo } from "react"; import { debounce } from "../utils/tinyLodash"; import useLatest from "./useLatest"; import useUnmount from "./useUnmount"; function useDebounceFn(fn, options) { const fnRef = useLatest(fn); const wait = (options === null || options === void 0 ? void 0 : options.wait) ?? 1000; const debounced = useMemo(()=>debounce((...args)=>{ return fnRef.current(...args); }, wait), // eslint-disable-next-line react-hooks/exhaustive-deps []); useUnmount(()=>debounced.cancel()); return { run: debounced, cancel: debounced.cancel }; } export default useDebounceFn;