Full lesson
Explore the full explanation, examples, and visuals at your own pace.
Counter.jsx
Click +1 once, and Counter runs again, but App doesn't. Why? Counter owns count through useState, so that's where the update lands. As we trace it, watch three components: Label and Hint, which Counter renders, and Header, which sits beside Counter under App.
function App() {
return <main><Header /><Counter /></main>;
}
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<Label value={count} />
<Hint />
<button onClick={() => setCount(count + 1)}>+1</button>
</div>
);
}Step 1: setCount marks Counter
Calling setCount doesn't touch the DOM. It queues an update carrying count 1 on Counter, and Counter goes from clean to needing a render. Only then does React call Counter again, and since Counter's output includes Label and Hint, they get called too.
Who runs again after one click
Walk the table. App and Header sit above the state, so they skip. Counter owns count, so it re-runs, and Label re-runs because its value goes from 0 to 1. Hint is the surprise. It has no props at all, and it still re-runs.
Rendering flows down, never up
A re-render flows downward from the component that owns the state. Counter's update spills into Label and Hint, and React doesn't check Hint's props first, so it runs anyway. App and Header stay dry. By default, an update re-runs the owner and everything beneath it.
Step 2: diff old output vs new
Now React compares the old output with the new. Same element types in the same positions are kept, then their props and text are checked. The Label span goes from 0 to 1, so that's an update. Hint and the button produce identical output, so both come out the same.
Step 3: commit one text change
Everything up to the Diff happens in memory. Counter, Label and Hint run, and React compares the new output with the old. Then commit writes just one text node into the DOM, which changes from showing 0 to showing 1. Running a component is not the same as repainting it.
Opting out: memo
Wrapping Hint in memo makes React shallow-compare its props before re-running it. With no props, they're equal, so Hint is skipped. But an inline function creates a new reference every render, so the comparison fails and Hint runs anyway. Stabilize that function with useCallback to keep memo working.
const Hint = memo(function Hint() {
return <p>Tap +1</p>;
});
// Inside Counter:
<Hint /> // skipped: props equal
<Hint onTap={() => {}} /> // runs: new function each timeRendering cascades down from the state owner; diffing keeps DOM writes minimal.
A state update re-runs the component that owns it and everything beneath it, never its ancestors or siblings. Then diffing keeps the DOM writes to real differences. So keep state low in the tree, close to where it's used, and fewer components sit underneath it to re-run.
