简体   繁体   English

React hooks useCallback 依赖于函数

[英]React hooks useCallback has dependency on function

I have below function我有以下功能

const handleClick = React.useCallback(
    (value) => {
      dispatch({ value });
    },
    [dispatch]
  );

My question is, in my case, is it necessary to have useCallback ?我的问题是,就我而言,是否有必要使用useCallback how React determines if the dependency is changed when the dependency is a function?当依赖是一个函数时,React 如何判断依赖是否改变?

  1. It is not necessary to use useCallback but using that will reflect in good performance.没有必要使用useCallback但使用它会反映良好的性能。

  2. useCallback returns a memoized callback. useCallback返回一个记忆化的回调。 According to my understanding, react invokes the function and only execute the callback function if there is any change in the dependencies that we have passed to it.根据我的理解, react调用该函数,并且只有在我们传递给它的依赖项发生任何变化时才执行回调函数。

References:参考:

  1. useCallback使用回调

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Hooks useCallback 依赖无限循环 - React Hooks useCallback dependency infinite loop 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 Hook useCallback 有一个不必要的依赖:'price'。 排除它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useCallback has an unnecessary dependency: 'price'. Either exclude it or remove the dependency array react-hooks/exhaustive-deps 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 有一个不必要的依赖:'GqlUserResponse' - React Hook useCallback has an unnecessary dependency: 'GqlUserResponse' React Hooks - useCallback 性能 - React Hooks - useCallback performance React 钩子函数依赖 - React hooks function 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM