[英]Using react Hook, shows missing dependency in case of useCallback
I am learning the hooks in ReactJs and stuck to some kind of warnings like dependencies.我正在学习 ReactJs 中的钩子,并坚持使用某种警告,如依赖项。 Here in src/pages/home , I am using useCallback in it.
在src/pages/home 中,我在其中使用了 useCallback 。 And One more question, Could you please give me real life condition where I need to use useCallback and useMemo, in my project in future.
还有一个问题,你能否给我一个真实的生活条件,我需要在未来的项目中使用 useCallback 和 useMemo。 Means, When I should go for useMemo and when to go for useCallback.Thanks.
意思是,什么时候我应该去 useMemo,什么时候去 useCallback。谢谢。
You need to provide a dependency array as second parameter.您需要提供一个依赖数组作为第二个参数。 Please refer to the official React docs for hooks
有关钩子的信息,请参阅React 官方文档
As a code example作为代码示例
const memoizedCallback = useCallback(
() => {
doSomething(a, b);
},
[a, b],
);
please note the second parameter of useCallback, the dependency array.请注意 useCallback 的第二个参数,依赖数组。
In general useMemo
is used for memoized values and by convention useCallback
is used for memoized functions.通常
useMemo
用于记忆值,并且按照惯例useCallback
用于记忆函数。 They are very similar.它们非常相似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.