React Hooks introduced the capability to use state and other lifecycle features while using functional components instead of classes. Hooks are a s...
For further actions, you may consider blocking this person and/or reporting abuse
Thanks for the article. The illustrations make it easy to follow.
I think the above statement is a little confusing. What occurs when the
deps
change? Is it: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 :)
Thanks for the feedback. I think it will be clearer when you update it.
Great article.
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!
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 :)
Nice stuff!
Where did you made those thumbnails and pictures?
If any website, do share!
Thank you! I drew the illustrations using an app called Paper Pro :)
Thank you! I like those illustrations!
I will use it on my youtube videos!
😅😅😅
The app is super easy to use and it has a free version as well. Definitely very useful to draw quickly!
Generally, I use to make some sketch like designs by excalidraw application...
Now, I find some new tech.
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.
Glad you found this post helpful and thanks for reading! :)
Thx for sharing !
No problem Nuno! I will be exploring a few other React hooks in the next couple weeks :)
Love the little illustrations!
Thanks Bennett!
Thanks!!
No problem Patricio :) thanks for reading!
Thanks. Milu
What would be the next post about?
I look forward to your next one
Thanks again
Hi there! I'm thinking to explore useRef next but I'm open for suggestions. Thank you for reading! :)
Damn. I wish I'd read this before my most recent interview lol. Thank you, Milu!
oh no! Now you are ready to conquer the next one though. Thanks for reading :)
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?
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.Very clear explanation, thanks for sharing!
Thanks Alberto! :)
Hey, Milu! Amazing article and writing style! Can I translate it to Portuguese (Giving you the proper reference)?
Hi Rodolpho, yes you can translate it to Portuguese. Send me the link when its ready :)