DEV Community

Discussion on: Demystifying React Hooks: useCallback and useMemo

Collapse
nibble profile image
Joseph Mawa

Thanks for the article. The illustrations make it easy to follow.

useCallback hook returns the same instance of the function being passed instead of creating a new one when a component re-renders. This occurs only when the dependencies passed to the second parameter change.

I think the above statement is a little confusing. What occurs when the deps change? Is it:

  • Returning the same instance of the callback?
  • Creating a new instance of the callback?
Collapse
milu_franz profile image
Milu Author • Edited on

Hi Joseph! Thanks for your question... I can see how this statement is confusing.

Let’s take a look at useCallback’s structure. It receives two parameters:

(#1) a function
(#2) an array of dependencies

useCallback returns the same instance of the function being passed (parameter #1) instead of creating a new one every time a component re-renders.

It creates a new instance of the function being passed (parameter #1) only when the array of dependencies (parameter #2) changes.

Does this make sense? I will update my post to make sure this definition is more clear. Thanks for your feedback :)

Collapse
nibble profile image
Joseph Mawa

Thanks for the feedback. I think it will be clearer when you update it.