{ "$schema": "https://ui.shadcn.com/schema/registry-item.json", "name": "useMemoizedFn", "title": "useMemoizedFn", "description": "useMemoizedFn hook", "registryDependencies": [ "https://shadcn-ahooks.vercel.app/r/utils.json" ], "files": [ { "path": "packages/hooks/src/useMemoizedFn/index.ts", "content": "import { useMemo, useRef } from 'react';\nimport { isFunction } from '../utils';\nimport isDev from '../utils/isDev';\n\ntype noop = (this: any, ...args: any[]) => any;\n\ntype PickFunction = (\n this: ThisParameterType,\n ...args: Parameters\n) => ReturnType;\n\nconst useMemoizedFn = (fn: T) => {\n if (isDev) {\n if (!isFunction(fn)) {\n console.error(`useMemoizedFn expected parameter is a function, got ${typeof fn}`);\n }\n }\n\n const fnRef = useRef(fn);\n\n // why not write `fnRef.current = fn`?\n // https://github.com/alibaba/hooks/issues/728\n fnRef.current = useMemo(() => fn, [fn]);\n\n const memoizedFn = useRef>(undefined);\n\n if (!memoizedFn.current) {\n memoizedFn.current = function (this, ...args) {\n return fnRef.current.apply(this, args);\n };\n }\n\n return memoizedFn.current;\n};\n\nexport default useMemoizedFn;\n", "type": "registry:file", "target": "src/hooks/ahooks/useMemoizedFn/index.ts" } ], "type": "registry:hook" }