React.memo helps your app run faster by remembering a component's output. It skips redoing work if the inputs (props) stay the same.
React.memo usage
const MemoizedComponent = React.memo(Component);name prop changes.const Hello = React.memo(function Hello({ name }) { return <p>Hello, {name}!</p>; });
onClick or label changes.const Button = React.memo(({ onClick, label }) => { return <button onClick={onClick}>{label}</button>; });
const CustomCompare = React.memo(Component, (prevProps, nextProps) => { return prevProps.value !== nextProps.value; });
This example shows a memoized CountDisplay component that only re-renders when the count changes. Typing in the input does not cause CountDisplay to re-render, improving performance.
import React, { useState } from 'react'; const CountDisplay = React.memo(({ count }) => { console.log('CountDisplay rendered'); return <p>Count: {count}</p>; }); export default function Counter() { const [count, setCount] = useState(0); const [text, setText] = useState(''); return ( <div> <CountDisplay count={count} /> <button onClick={() => setCount(count + 1)}>Increase Count</button> <input type="text" value={text} onChange={e => setText(e.target.value)} placeholder="Type here" aria-label="Input text" /> </div> ); }
React.memo only works with functional components.
It does a shallow comparison of props by default, so objects or arrays as props may need custom comparison.
Use React DevTools to see when components re-render to check if memoization helps.
React.memo remembers a component's output to skip unnecessary re-renders.
Use it to improve performance when props rarely change.
You can provide a custom function to control when to re-render.
Practice
What is the main purpose of React.memo in React?
Solution
Step 1: Understand React.memo's role
React.memo is a higher-order component that wraps a functional component to optimize rendering.Step 2: Identify its effect on rendering
It skips re-rendering the component if the props are the same as the previous render.Final Answer:
To prevent unnecessary re-rendering of a functional component when its props have not changed -> Option CQuick Check:
React.memo optimizes rendering = A [OK]
- Thinking React.memo manages state
- Confusing React.memo with class components
- Assuming React.memo fetches data
Which of the following is the correct syntax to wrap a functional component MyComponent with React.memo?
Solution
Step 1: Recall React.memo usage syntax
React.memo is a function that takes a component and returns a memoized version.Step 2: Check each option's syntax
const MemoizedComponent = React.memo(MyComponent); correctly calls React.memo with the component reference without invoking it.Final Answer:
const MemoizedComponent = React.memo(MyComponent); -> Option AQuick Check:
React.memo(Component) syntax = D [OK]
- Calling component inside React.memo (using parentheses)
- Using arrow function syntax incorrectly
- Using braces instead of parentheses
Consider this code snippet:
const MyComponent = React.memo(({ count }) => {
console.log('Rendered');
return <div>Count: {count}</div>;
});
function App() {
const [count, setCount] = React.useState(0);
const [text, setText] = React.useState('');
return (
<>
<MyComponent count={count} />
<input value={text} onChange={e => setText(e.target.value)} />
</>
);
}What will be logged to the console when the user types in the input box?
Solution
Step 1: Understand React.memo behavior with props
MyComponent is memoized and only re-renders if its props change. It receivescountas prop.Step 2: Analyze prop changes on input update
Typing changestextstate, causing App to re-render. However, thecountprop remains unchanged (same primitive value), so React.memo skips re-rendering MyComponent.Step 3: Check if console logs on input change
Sincecountdoes not change, React.memo prevents re-render, so 'Rendered' logs only once on mount.Final Answer:
'Rendered' logs only once when the component mounts -> Option AQuick Check:
React.memo skips re-render if props unchanged = B [OK]
- Assuming any parent state change triggers child re-render
- Ignoring React.memo's prop comparison
- Thinking console logs every input change
Consider this usage of React.memo:
const MyComponent = React.memo(({ value }) => {
return <div>Value: {value}</div>;
}, (prevProps, nextProps) => {
return prevProps.value === nextProps.value;
});Which of the following correctly describes this code?
Solution
Step 1: Understand React.memo's custom comparison function
The second argument is a function that returns true if props are equal, meaning skip re-render.Step 2: Analyze the provided function
The function returns true whenprevProps.value === nextProps.value, which is correct to skip re-render when props are equal.Step 3: Confirm the correct description
The custom comparison function returns true when props are equal, which prevents re-rendering correctly accurately describes that it returns true when props are equal, preventing re-rendering correctly.Final Answer:
The custom comparison function returns true when props are equal, which prevents re-rendering correctly -> Option DQuick Check:
Custom compare returns true to skip re-render = C [OK]
- Thinking custom compare should return false when props equal
- Believing React.memo can't take a second argument
- Confusing arrow function usage
You have a component wrapped with React.memo that receives a prop data which is an object. You notice it re-renders every time the parent renders, even if data has the same content. How can you fix this?
Solution
Step 1: Understand why re-renders happen with object props
Objects are compared by reference, so even if content is same, new object causes re-render.Step 2: Use custom comparison function
React.memo accepts a second argument to compare props deeply and prevent re-render if content is equal.Step 3: Evaluate other options
Converting to string is inefficient and error-prone; wrapping parent doesn't solve child re-render; useState inside child doesn't prevent re-render.Final Answer:
Use a custom comparison function in React.memo to do a deep comparison ofdata-> Option BQuick Check:
Deep compare object props with custom function = A [OK]
- Passing new object each render without memoization
- Ignoring object reference equality
- Trying to fix with useState inside child
Ready to prove it?
Pass the test and this pattern moves unknown
