Establish good practices around useCallback, useMemo and memo
See original GitHub issueSummary
Follow-up ticket from Slack discussion around useCallback
, useMemo
and memo
: https://xwp.slack.com/archives/CCCAUAH6F/p1643966654671809
https://github.com/GoogleForCreators/web-stories-wp/pull/10388#pullrequestreview-872524783
We want to establish good practices around them, we need to choose the direction e.g. removing some of the unnecessary usages or going all-in. We might want to compare those approaches CPU/memory-wise (cost of all-in approach).
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
How to useMemo and useCallback: you can remove most of ...
What is the purpose of useMemo and useCallback hooks, mistakes and best practices in using them, and why removing most of them might...
Read more >Understanding useMemo and useCallback - Josh W Comeau
The fundamental idea with useMemo is that it allows us to “remember” a computed value between renders. This definition requires some unpacking.
Read more >When to useCallback and useMemo in our React projects?
Today we will talk about when to useCallback and useMemo React hooks in our projects. Memoization. First of all, we need to define...
Read more >React useMemo vs. useCallback: A pragmatic guide
React provides two different methods to help developers circumvent non-code-related performance issues: useMemo and useCallback.
Read more >How to use react React.memo, useMemo, useCallback correctly
Purpose As I try to learn about how to optimize my app to work as a professional, I'm learning React.memo, useMemo, useCallback as...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Not this sprint at least, just cleaning up the New Issues column currently.
I think a lot of our
useCallbacks
should just be actions straight from the story reducer if possible.Right now we have a lot of this in the codebase:
which should really just be:
We really shouldn’t see re-renders in components when the re-render is happening just to update an action in the component.
As far as
memo
goes, I think it’s fine to use in 3 scenarios:[...].map(...)
callI’m not a fan of this article mentioned to memoize all things. I think memoizing everything just adds to the complexity of the code & what updates when, and doesn’t fix any of the root issues which all stem around passing down data wisely (whether through props or hooks) https://attardi.org/why-we-memo-all-the-things/
I think
useCallback
&useMemo
should be used in providers or when passing to a memoized component, but it’s important to think if they’re actually doing anything. Most of the time the wins are around minimizing the source we’re getting state through, than the downstream dependencies consuming said state