Skip to content

Commit

Permalink
fix: useMemoizedFn return a plain function type (alibaba#1470)
Browse files Browse the repository at this point in the history
Co-authored-by: 砖家 <brickspert.fjl@antfin.com>
  • Loading branch information
fanck0605 and brickspert authored Feb 25, 2022
1 parent 58fd63f commit 3618ae6
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/hooks/src/useMemoizedFn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { useMemo, useRef } from 'react';

type noop = (this: any, ...args: any[]) => any;

type PickFunction<T extends noop> = (
this: ThisParameterType<T>,
...args: Parameters<T>
) => ReturnType<T>;

function useMemoizedFn<T extends noop>(fn: T) {
if (process.env.NODE_ENV === 'development') {
if (typeof fn !== 'function') {
Expand All @@ -15,11 +20,11 @@ function useMemoizedFn<T extends noop>(fn: T) {
// https://github.com/alibaba/hooks/issues/728
fnRef.current = useMemo(() => fn, [fn]);

const memoizedFn = useRef<T>();
const memoizedFn = useRef<PickFunction<T>>();
if (!memoizedFn.current) {
memoizedFn.current = function (this, ...args) {
return fnRef.current.apply(this, args);
} as T;
};
}

return memoizedFn.current;
Expand Down

0 comments on commit 3618ae6

Please sign in to comment.