In this article, we will discuss what the React.memo()
function is, how it works, and when to use it in your React apps. Initiating our adventure..
What is the React.memo() function?
React components are programmed to re-render upon the modification of state or props value. Although, this feature may hamper your app's performance as it causes all child components associated with the parent component to also re-render, even if the alteration was meant only for the parent component. The parent component's re-rendering triggers the re-rendering of all its children components as well.
The React.memo()
function is a higher-order component (HOC) provided by React. It is used to memoize a component, which means that it caches the output of a component and returns it when the input props remain the same. The memoized component will only be re-rendered if its props have changed.
The React.memo()
function is similar to the shouldComponentUpdate()
lifecycle method, which is used to control when a component should be re-rendered. React.memo()
is a simpler and more efficient way to achieve this, as it does not require you to implement the shouldComponentUpdate()
method.
How does the React.memo() function work?
When you use the React.memo()
function to wrap a component, React will automatically compare the new props with the previous props. If the props are the same, React will use the cached output of the component and will not re-render it. If the props have changed, React will re-render the component and update the cached output.
To show you what I mean..
MyComponent
component is memoized using the React.memo()
function. This means that if the title and text props remain the same, the component will not be re-rendered.
To implement the memo
function on a component at the export level, you can use an alternative approach:
What is memoization?
Memoization caches the results of expensive functions to prevent repetitive computation and avoid redundant calls. This technique saves time and resources by storing the calculated results for future use.
When to use the React.memo() function?
The React.memo()
function should be used when you have a component that receives props that do not change frequently. Memoizing such a component can improve performance by preventing unnecessary re-renders.
Notice this also, it is important to note that memoizing a component may not always improve performance. If a component receives props that change frequently, memoizing it may actually degrade performance, as React will spend more time checking for prop changes and updating the cached output.
It is also worth noting that memoizing a component is not a silver bullet for improving performance. There are other techniques, such as using the useCallback()
hook, that can also be used to optimize React components.
In the upcoming days, I plan to create a well-crafted article that will delve into the functionality of the useCallback
hook.
What is difference between React.memo function and useMomo() hook?
React.memo()
and useMemo()
are both optimization techniques provided by React, but they serve different purposes. Let's see how..
The main purpose of React.memo()
is to optimize the rendering of a component by caching its output based on its props. It's a higher-order component that you can use to wrap a component and enable this optimization.
On the otherside, the useMemo()
hook is used to optimize the performance of expensive computations within a component. It allows you to memoize the result of a function call and use the cached result in subsequent renders, as long as the dependencies of the function have not changed. The useMemo()
hook returns the cached result of the function call, which you can use in your component.
To show you what I mean..
To wrap things up, the React.memo()
function is a powerful tool that can help you optimize your React components. It allows you to memoize a component and cache its output, which can improve performance by preventing unnecessary re-renders. Even so, it should be used with care and only when it is appropriate for your specific use case..
🍀Support
Please consider following and supporting us by subscribing to our channel. Your support is greatly appreciated and will help us continue creating content for you to enjoy. Thank you in advance for your support!
Top comments (3)
Just remember, there are costs to using memo. You should refrain from using it on every component just because you can!
Take a look at the deep dive section of React's own documentation on useMemo. It explicitly discuss scenarios when it should be used
Excellent post! Looking forward to more :)
Thank you for your kind words.. I will create more engaging content that you will love. Stay tuned..