Skipping re-renders with React.memo
When a parent re-renders, React re-renders its children too — even when their
props did not change. That wasted work is what memo is for. Wrap a component in
it and React skips the re-render when the props are the same:
const Label = memo(function Label({ text }) {
return <p>{text}</p>;
});
React compares the new props with the previous ones, shallowly. If they are equal, it reuses the last rendered output instead of calling the component again.
The React Compiler now does most of this for you: it memoises components, so a
child no longer re-renders whenever its parent does. That makes memo an
escape hatch for the cases the compiler cannot see — most often a component
whose props include a new object, array, or function on every render, where you
want to assert equality yourself.
memo only helps when the props are stable. Pass a new object, array, or function
on every render and the comparison always fails, so the memo does nothing.
Your job is to wrap a child component in memo.
Challenge
A child re-renders with its parent even when nothing about it changed. Export an
App component from App.jsx that renders a child component wrapped in
memo, passing it the text Hello to show in a paragraph.
Define the child in the same file, wrap it in memo, and render it from App.
The Test renders the component and expects the paragraph to read Hello. The
starter renders Goodbye, so the Test fails.