DEV Community

Cover image for Demystifying React Hooks: useCallback and useMemo

Demystifying React Hooks: useCallback and useMemo

Milu on June 14, 2020

React Hooks introduced the capability to use state and other lifecycle features while using functional components instead of classes. Hooks are a s...
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 • Edited

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.

Collapse
 
rmuratov profile image
Ramil Muratov

Great article.

Adding performance optimizations using useCallback and useMemo is expensive

Wouldn't you mind to tell where can I read more about it? I saw some notes in official docs about using these only as performance optimization, but have never read any post or explanation from someone from core team or seen some benchmarks. Thanks!

Collapse
 
milu_franz profile image
Milu

Hi Ramil, thank you for reading! Let's break down some reasons why useCallback and useMemo are expensive:

Regular function: const testFunc = () => { ... do something }

useCallback: const testCallbackFunc = useCallback(() => { ... do something }, [dependencies]);

In the callback example, you are creating your regular function + calling a callback + passing an array of dependencies. It is already a bit more complex when looking at them side by side, but also the useCallback hook is setting properties and running through its own logic under the hood. So, if you are trying to optimize something super simple like the regular function I have in my example, useCallback and useMemo are just not worth it. We are creating complex code for ourselves and requesting our component to run other complex logic under the hood to optimize something very minimal.

Here is a fantastic article by Kent C Dodds that explains this in more detail.

I hope this explanation was helpful :)

Collapse
 
vuelancer profile image
Vuelancer

Nice stuff!
Where did you made those thumbnails and pictures?
If any website, do share!

Collapse
 
milu_franz profile image
Milu

Thank you! I drew the illustrations using an app called Paper Pro :)

Collapse
 
vuelancer profile image
Vuelancer

Thank you! I like those illustrations!
I will use it on my youtube videos!
😅😅😅

Thread Thread
 
milu_franz profile image
Milu

The app is super easy to use and it has a free version as well. Definitely very useful to draw quickly!

Thread Thread
 
vuelancer profile image
Vuelancer

Generally, I use to make some sketch like designs by excalidraw application...
Now, I find some new tech.

Collapse
 
vipinkumarsn4 profile image
Vipin Saini • Edited

Thanks Milu for another informative post.
Keep the good work continue.

But, one Question please -
What are the alternatives of these 2 functions please? because you have said in your post these both functions add performance optimizations and expensive.

Collapse
 
milu_franz profile image
Milu

Glad you found this post helpful and thanks for reading! :)

Collapse
 
nunocpnp profile image
Nuno Pereira

Thx for sharing !

Collapse
 
milu_franz profile image
Milu

No problem Nuno! I will be exploring a few other React hooks in the next couple weeks :)

Collapse
 
beznet profile image
Bennett Dungan

Love the little illustrations!

Collapse
 
milu_franz profile image
Milu

Thanks Bennett!

Collapse
 
psanchez1982 profile image
Patricio Sanchez Alvial

Thanks!!

Collapse
 
milu_franz profile image
Milu

No problem Patricio :) thanks for reading!

Collapse
 
ponyjackal profile image
ponyjackal

Thanks. Milu
What would be the next post about?
I look forward to your next one
Thanks again

Collapse
 
milu_franz profile image
Milu

Hi there! I'm thinking to explore useRef next but I'm open for suggestions. Thank you for reading! :)

Collapse
 
vunderkind profile image
mogwai

Damn. I wish I'd read this before my most recent interview lol. Thank you, Milu!

Collapse
 
milu_franz profile image
Milu

oh no! Now you are ready to conquer the next one though. Thanks for reading :)

Collapse
 
enricoschaaf profile image
Enrico

Great article! I often see examples, where the submit function of a form is wrapped in a useCallback, but I dont see the reason why someone would do that, any idea?

Collapse
 
milu_franz profile image
Milu • Edited

Hello there! The only reason I can think of is that whoever is implementing the code, does not want their handleFormSubmit() function to be created every time the component re-renders. I found this article where they use the useCallback() hook in a handleFormSubmit() to stop tree invalidation issues. However, I haven't encountered issues when creating a form without using useCallback in the past.

Collapse
 
albertopdrf profile image
Alberto Pérez de Rada Fiol

Very clear explanation, thanks for sharing!

Collapse
 
milu_franz profile image
Milu

Thanks Alberto! :)

Collapse
 
rodolphoasb profile image
Rodolpho Bravo

Hey, Milu! Amazing article and writing style! Can I translate it to Portuguese (Giving you the proper reference)?

Collapse
 
milu_franz profile image
Milu

Hi Rodolpho, yes you can translate it to Portuguese. Send me the link when its ready :)