UNPKG

@schema-render/core-react

Version:

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

19 lines (18 loc) 589 B
/** * fork from https://github.com/alibaba/hooks/blob/master/packages/hooks/src/useMemoizedFn/index.ts */ import { useMemo, useRef } from "react"; export default function useMemoizedFn(fn) { const fnRef = useRef(fn); const memoizedFn = useRef(); // why not write `fnRef.current = fn`? // https://github.com/alibaba/hooks/issues/728 fnRef.current = useMemo(()=>fn, [ fn ]); if (!memoizedFn.current) { memoizedFn.current = function(...args) { return fnRef.current.apply(this, args); }; } return memoizedFn.current; }