0
0
Reactframework~5 mins

React.memo usage - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is React.memo used for in React?

React.memo is a function that helps to optimize functional components by preventing unnecessary re-renders when the component's props have not changed.

Click to reveal answer
beginner
How do you wrap a functional component with React.memo?
<p>You wrap the component by exporting it as <code>export default React.memo(ComponentName);</code> or by assigning <code>const MemoComponent = React.memo(ComponentName);</code></p>
Click to reveal answer
intermediate
What kind of comparison does React.memo use by default to check if props changed?

It uses a shallow comparison of the props, meaning it checks if the prop values are the same by reference or primitive equality.

Click to reveal answer
intermediate
How can you customize the prop comparison in React.memo?

You can pass a second argument to React.memo, a function that receives the old and new props and returns true if they are equal (skip re-render) or false if not.

Click to reveal answer
intermediate
When should you avoid using React.memo?

Avoid using React.memo if the component is very simple or if props change frequently, because the cost of comparing props might outweigh the benefits.

Click to reveal answer
What does React.memo do?
AAutomatically updates state
BPrevents re-render if props are unchanged
CConverts class components to functional
DHandles side effects
How does React.memo compare props by default?
ADeep comparison of all nested objects
BCompares only the first prop
CIgnores props and always re-renders
DShallow comparison of props
Which is a correct way to use React.memo?
AMyComponent.memo()
BReact.memo(MyComponent)();
Cconst MemoComp = React.memo(MyComponent);
DReact.memo = MyComponent;
What can you pass as a second argument to React.memo?
AA function to customize prop comparison
BA boolean to force re-render
CAn object with styles
DA string with component name
When might React.memo not improve performance?
AWhen props change frequently
BWhen component is complex
CWhen props rarely change
DWhen using class components
Explain how React.memo helps improve React app performance and when to use it.
Think about how React decides to update components.
You got /4 concepts.
    Describe how to customize prop comparison in React.memo and why you might want to do that.
    Consider cases where shallow comparison is not enough.
    You got /4 concepts.