简体   繁体   中英

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. Here in src/pages/home , I am using useCallback in it. 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. Means, When I should go for useMemo and when to go for useCallback.Thanks.

You need to provide a dependency array as second parameter. Please refer to the official React docs for hooks

As a code example

const memoizedCallback = useCallback(
  () => {
    doSomething(a, b);
  },
  [a, b],
);

please note the second parameter of useCallback, the dependency array.

In general useMemo is used for memoized values and by convention useCallback is used for memoized functions. They are very similar.

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