简体   繁体   中英

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 ? how React determines if the dependency is changed when the dependency is a function?

  1. It is not necessary to use useCallback but using that will reflect in good performance.

  2. useCallback returns a memoized callback. 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.

References:

  1. useCallback

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM