What is React memo?
React is a JavaScript library that is used to create interactive and dynamic user interfaces. It divides the code into components, reducing redundancy and enhancing efficiency to maximize user experience. React memo is a React feature that helps us optimize our code.
In this answer, we will discuss React memo in detail.
React memo
React memo is a higher-order component that makes our React code more efficient. A higher-order component is a function that wraps around a component to provide additional features to it. While using React memo, we wrap our functional components in React.memo() function to stop continuous re-rendering of our component if it has not changed its state or props.
We can simply say it memoizes our component's result unless it has changed and displays the same output whenever the component is called. This helps us to avoid unnecessary re-rendering and improves the overall efficiency of our React application, making them faster and more efficient.
Syntax
There are two ways we can use React memo:
We call the
React.memofunction at the time of component declaration.
const MemoizedComponent = React.memo((props) => {// We write component logic and rendering here});
MemoizedComponent: It refers to our functional component.React.memo: It refers to React memo function to makeMyComponentmemoized.props: It refers to the object we receive when our component is called by the parent component.
We call the
React.memofunction for our component and store it in a variable. then we use this variable with ourexportstatement at the end.
const MemoizedComponent = React.memo(MyComponent);export default MemoizedComponent;
MemoizedComponent: It refers to the constant variable representing the memoized component.React.memo: It refers to the function which makes ourMyComponentmemoized.MyComponent: It refers to the component we want to memorize.export default MemoizedComponent: We export ourMemoizedComponentas the default export of the module.
Coding Example
The following code example demonstrates how we can use React memo.
Let's understand what happens in ParentComponent:
Line 1: We import all the necessary dependencies.
Line 2: We import the
ChildComponent.Line 5: We declare a use state called
text.Lines 7–9: We define a function
displaywhich sets the value oftextto a string that we want to display on the screen.Lines 10–12: We define a function
hidewhich sets the value oftextto null.Lines 14–19: We call our
ChildComponentwrapped with React memo. We set themessageprop for ourChildComponent. Then we create two buttons which display and hide a string.Line 12: We export the
ParentComponentas the default export of the module.
Now let's understand what happens in ChildComponent:
Line 1: We import the
React.Lines 3–5: We declare a functional component called the
ChildComponent. Then we wrap it withReact.memo()so that unless the prop or component's state is changed, there won't be any re-rendering of our component. So whenever we click the display or hide button in the parent component, our child component will not be re-rendered as its props don't change.Line 7: We export the
ChildComponentas the default export of the module.
Conclusion
React memo is an extremely helpful tool for optimizing functional components in our React applications. By wrapping a component with React.memo(), we can avoid constant re-rendering of our component if it receives the same props. This results in improved performance and is more suitable for complex components.
Free Resources