React Learning Guide
Boosting React Performance: useCallback vs. useMemo Hooks
React's useCallback and useMemo hooks are crucial for op...
For further actions, you may consider blocking this person and/or reporting abuse
Nice article.
One point of feedback. Examples in best practices articles can often be seen as good practices so ensure they are optimal as well.
This code has a hidden bug as the state can be stale if a user is clicking fast
A better way to write this is
Given that the example is trying to showcase the dependency array then maybe rework the example to use another dependency
Great read! Looking forward to more like these!
I will be posting relevant topics to 'Mastering React' and adding appropriate links to each topic.
Thank you, I will try these tips
Very informative, thanks for sharing.
This is not 100% correct, the child component will still rerender since his father state changed.
In order to prevent rerender of the child you will have to memoize the child ( export it with memo(ChildComponent)) so that this will only rerender if the props change.
Then as the article say you need to wrap function inside a usecallback so that when the father component rerender the function that will be passed as props wont change, thus the child component will not rerender.
Thank you for your feedback! 🙏
You're absolutely right. I should have included the step about memoizing the child component using React.memo to fully prevent it from re-rendering when the parent state changes.
This ensures that:
Thanks again for pointing this out! 🙌
This rebuttal is not true.
If the props don't change then react will not rerender the child component even if the parent state changes.
Rerendering child components would be a massive performance hit for the tree if their props don't change
To prevent rerendering of child component you must first ensure it's props are stable meaning they only change when they need to.
If you need to further optimize when the component rerenders then you can use React.memo to exclude some props from consideration or change the default equality check that react uses which is
Object.is
Object.is
is a reference equality checkAdding React.memo on a component without first optimizing it's props will not have any effect
This can be easily tested by adding console logs in a component and changing the state in the parent and seeing if the child component renders a log. Or just use react dev tools.
This is all discussed on the react docs
react.dev/reference/react/memo
The rebuttal you received is mostly correct when they argue that React won't re-render the child component if the props haven't changed. Indeed, React.memo should only be used after ensuring the props passed to the child are stable. However, useCallback plays a role in keeping the function reference stable—a common scenario that could otherwise lead to unwanted re-renders.
Yep that is my point
useMemo and useCallback will be deprecated in React 19, FYI!
Thanks for the heads-up! 🙌
While it's good to stay informed about upcoming changes, a lot of companies are still running on older versions like React 16 or 17. In fact, some organizations take years before upgrading to the latest major versions due to stability concerns or the size of their codebase.
So, even though useMemo and useCallback might be deprecated in React 19, these hooks will likely still be widely used in production environments for quite a while. 😊
But it's always good to be aware of what's coming—thanks again for sharing! 🙏
I don't think they will be deprecated. The react compiler just automatically adds them for you.
That's not really a deprecation