简体   繁体   English

使用 react Hook,在 useCallback 的情况下显示缺少的依赖项

[英]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.

相关问题 React Hook useCallback 缺少依赖项: - React Hook useCallback has a missing dependency: React Hook useEffect/useCallback 缺少依赖项 - React Hook useEffect/useCallback has a missing dependency React Hook useCallback 缺少依赖项警告,但存在依赖项 - React Hook useCallback has a missing dependency warning, but the dependencies are present 如何正确修复 React Hook useCallback 缺少依赖项 - How to properly fix React Hook useCallback has a missing dependency React Hook useCallback 有一个不必要的依赖:'GqlUserResponse' - React Hook useCallback has an unnecessary dependency: 'GqlUserResponse' Cube.js React Hook React.useCallback 缺少一个依赖项:'pivotConfig'。 + - Cube.js React Hook React.useCallback has a missing dependency: 'pivotConfig'. + 反应:useCallback - useCallback 与空依赖数组 VS 根本不使用 useCallback - React: useCallback - useCallback with empty dependency array VS not using useCallback at all React Hook useCallback 缺少依赖项。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - React Hook useCallback has a missing dependency. Either include it or remove the dependency array react-hooks/exhaustive-deps React useCallback linting 错误缺少依赖项 - React useCallback linting error missing dependency 反应 useEffect 和 useCallback linting 错误 - 缺少依赖项 - React useEffect and useCallback linting error - missing dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM