Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useMemoizedFn 持久后的函数不会继承函数本身的属性? #2273

Closed
pokerboard opened this issue Aug 1, 2023 · 2 comments · Fixed by #2281
Closed

useMemoizedFn 持久后的函数不会继承函数本身的属性? #2273

pokerboard opened this issue Aug 1, 2023 · 2 comments · Fixed by #2281
Labels
documentation Improvements or additions to documentation

Comments

@pokerboard
Copy link

import { useMemoizedFn } from 'ahooks';
import { useCallback } from 'react';

const fn = () => {};

fn.childVar = 'childVar';
fn.childFn = () => {};

const Demo: React.FC = () => {
  console.dir(fn);
  console.dir(useMemoizedFn(fn));
  console.dir(useCallback(fn, []));

  return <></>;
};

export default Demo;

image

@liuyib
Copy link
Collaborator

liuyib commented Aug 1, 2023

useMemoizedFn 返回的是与传入的 fn 完全不同的一个新函数(只是在这个新函数里调用了传入的 fn),并一直保持这个新函数的引用。

所以,经过 useMemoizedFn 包裹的 fn,就不再是原来的 fn 了。

@liuyib
Copy link
Collaborator

liuyib commented Aug 1, 2023

需要在函数上加一些自定义属性的话,这个场景 useMemoizedFn 目前满足不了,还是使用 useCallback 吧。

后面我在文档里说明这一点。

@liuyib liuyib added the documentation Improvements or additions to documentation label Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants