Sub topic 2
How concurrent rendering keeps the UI responsive
This chapter assumes you know function components, useState, and how a state change makes React re-render a component and its children. It also assumes you know the split between urgent updates (typing, clicking) and non-urgent updates from the previous chapter. Here you will see the mechanism that lets React act on that split.
The problem: rendering that cannot stop
Browsers run your JavaScript, handle input, and paint on one main thread. Before React 18, a render was a single uninterrupted task. Once React started rendering a tree, nothing else could run until it finished. If the render took 400 ms, the page ignored clicks and keystrokes for 400 ms.
This component shows the problem. Each SlowItem busy-waits for 1 ms to imitate expensive work, so 500 items cost about half a second per render.
import { useState } from 'react';
function SlowItem({ text }) {
const start = performance.now();
while (performance.now() - start < 1) {
// Busy-wait for 1 ms to imitate expensive rendering.
}
return <li>{text}</li>;
}
export default function App() {
const [text, setText] = useState('');
const items = Array.from({ length: 500 }, (_, i) => i);
return (
<>
<input value={text} onChange={(e) => setText(e.target.value)} />
<ul>
{items.map((i) => (
<SlowItem key={i} text={text} />
))}
</ul>
</>
);
}Every keystroke re-renders all 500 items before the input can show the new character, so typing feels sluggish. The following chapters fix this without changing SlowItem.
Rendering in small, pausable units
Concurrent rendering changes how React does the work, not what it computes. React splits a render into many small units, roughly one component at a time. Between units it hands control back to the browser and checks whether anything more important has arrived.
This gives React three abilities:
- Pause. React can stop in the middle of a render and let the browser handle input or paint.
- Resume. When the thread is free, React continues where it stopped.
- Abandon. If a more urgent update arrives, React can throw away the half-finished work and start again from the latest state.
Pausing and abandoning are safe because React keeps render and commit separate. The render phase only calculates what the UI should look like. Nothing in the DOM changes during it. The commit phase applies the result to the DOM, and it runs synchronously and never partially. The user never sees a half-rendered tree.
Synchronous and concurrent rendering compared
| Aspect | Synchronous render | Concurrent render |
|---|---|---|
| Can be interrupted | No | Yes, between units of work |
| Input during a slow render | Waits until the render ends | Handled promptly |
| Work may be discarded | No | Yes, if a newer update arrives |
| DOM updated | Once per render | Only at commit, never partially |
React still chooses synchronous rendering for urgent updates, such as the update from a click or keystroke. Interruptible rendering is used only for updates that you mark as non-urgent, which the next chapters cover.
What this means for your code
Because React may call a component several times and discard some results, render must be pure. Don't mutate variables outside the component or send requests during render. Put side effects in event handlers or useEffect. Components that follow this rule already work with concurrent rendering.
Exercise
A user types in an input while React is rendering a slow, non-urgent list. Why can React drop the partly finished list render without showing the user a broken UI?
Show answer
The render phase only computes the next UI and does not touch the DOM. Only the commit phase changes the DOM, and it runs synchronously and in one piece. React can therefore discard unfinished render work and restart it with the latest state, and the user only ever sees complete commits.
Exercise
Why must components be pure for concurrent rendering to work correctly?
Show answer
React may call a component more than once and discard some of the results. A side effect in render, such as mutating an outside variable, could then run more times than expected or for a render that never commits. Side effects belong in event handlers or effects.