The note's state lives in App. Every keystroke changes App's state, so App
re-renders, and so does everything it renders, including the 30 ms chart. The
chart doesn't use the note at all:
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Before reaching for memo
You could wrap Chart in memo. That works, but it treats the symptom. The
cause is that state is higher than it needs to be. Only the input and the
character count use note, so move them, and the state, into their own
component:
App then renders <NoteBox /> inside the Note profiler, and has no state.
Make the change, clear the console and type two letters.
What you'll see
Only Note update: 0 ms, twice. Typing re-renders NoteBox and nothing else.
The chart costs nothing, without any memo.
This is state colocation: keep state as close as possible to the components
that use it. It's the cheapest performance fix there is, and it usually makes
code clearer too.
When state can't move down: pass children
Sometimes the state must stay high, because a wrapper uses it, such as a panel
that can be pinned to stand out. Its contents can still escape the re-render
if they're passed in as children:
<Chart /> is created by the parent of Pinnable. When Pinnable's state
changes, its children prop is the same element as before, so React doesn't
render Chart again. Only components created inside the one that changed are
re-rendered.
Exercise
Pin the chart
Add Pinnable to App.jsx, and wrap the chart in it inside the Chart
profiler, as above. Click Pin and Unpin, and read the Chart lines.
Then change Pinnable so it renders <Chart /> itself, in its body, instead of
children, and click again. Explain the difference in the two numbers.
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
When state changes, React re-renders the component that owns it and every
element that component creates in its JSX. An element that arrived as a
prop was created by someone else, during an earlier render, and hasn't changed.
Hint 2: a first step
Do the children version first. A full chart render costs about 30 ms, so a
Chart update line far below that means Chart itself didn't run, even
though the profiler around it reports that something in its subtree updated.
Then move <Chart /> into Pinnable's body and compare.
With children, each click logs Chart update: 0 ms: Pinnable re-rendered
(the button and Pinned changed), but the <Chart /> element it was given is
the same object as last time, so React skipped it. With <Chart /> written
inside Pinnable, each click logs about Chart update: 30 ms: Pinnable
creates a new <Chart /> element on every render, so the chart renders again.
The same trick doesn't help if the wrapper removes its children, such as a
collapsible panel with {open && children}. Closing it unmounts the chart, and
opening it mounts a new one, which costs the full 30 ms. To keep a hidden
subtree mounted, use Activity, in this day's last lesson.
Completion is saved in this browser only. It does not measure mastery.