Why it re-rendered: the whole lesson in words
Typing a reply in a support inbox re-renders every ticket row.
Mei's slow inbox
Mei: My inbox feels slow when I type a reply.
Dev: One key should only touch the reply box. Let me count what renders.
Before guessing why something is slow, count what renders.
One key
Dev: Here is the inbox the agent wrote.
Agent: Inbox keeps the draft text in state. ReplyBox shows it.
Afterdiff: Mei presses one key in the reply box. How many components render?
Commit to a number before you look.
Who renders whom
Afterdiff: Each box is a component. Inbox draws TicketList and ReplyBox, and TicketList draws five TicketRows. The draft text lives in Inbox, at the top.
Components form a tree: a component renders the components it returns.
One key, traced
Afterdiff: Mei presses H. ReplyBox calls setDraft, so Inbox renders: its state changed. Then every component under it renders, because its parent did.
Dev: Eight renders, and no ticket changed.
A state change renders its owner, and by default every component under it.
Render, then commit
Afterdiff: Mei presses i. Eight components render again, but the commit changes one thing on the page: the text in the reply box. Rendering is React calling your components. Committing is changing the DOM.
Rendering is calling components; React changes only the DOM nodes that differ.
The agent's memo
Dev: Can you stop the rows from re-rendering?
Agent: Done. I wrapped TicketRow in memo, so it skips a render when its props are the same.
Afterdiff: Mei presses H again. How many rows render now?
memo compares props; it does not move state.
Memo sees new props
Afterdiff: All five render, and the reason says why: onOpen changed. TicketList makes a new arrow function for every row on every render, and memo compares each prop with Object.is.
Agent: A new arrow is never the same object, so my memo never skips.
An inline function is a new prop every render, so memo cannot skip.
Move the state down
Dev: Keep the draft where it is typed.
Afterdiff: ReplyBox now owns the draft. Mei presses H. ReplyBox renders because its state changed, and its parent does not render, so nothing else does. No memo needed.
State kept where it is used renders only what uses it.
It keeps up
Afterdiff: Mei presses i: one render, one commit.
Mei: It keeps up with me now.
Agent: Keep state where it is used.
Fix where the state lives, not the components that render because of it.
The rule
Afterdiff: A component renders when its state changes or its parent renders. memo skips it only when every prop is the same object as last time. Keep state where it is used.
State or parent renders a component; memo needs the same props; keep state low.
The common wrong idea
A React component re-renders only when its props change, so wrapping a component in memo stops extra renders.
What really happens
A component renders when its state changes or its parent renders. memo skips a render only when every prop is the same object as last time (Object.is), so new functions or objects defeat it. Keeping state where it is used renders less.
The one-page cheatsheet for this lesson →