Sub topic 2
How Concurrent Rendering Keeps the UI Responsive
This chapter explains the mechanism behind transitions and deferred values: rendering that React can pause, abandon and restart. It assumes you are comfortable with function components, useState, props, and the idea that a state change makes React re-render a component and its children. The previous chapter separated urgent updates from non-urgent ones. This chapter explains how React acts on that difference.
The problem: rendering blocks the main thread
A browser runs your JavaScript, handles clicks and key presses, and paints the screen on a single thread, the main thread. While your code runs, none of those other jobs can happen.
Before React 18, a render was one uninterrupted task. React called your components, compared the results, and updated the DOM, all without a break. If a render took 300 ms, the page ignored input for 300 ms. Typing felt laggy, and clicks seemed to do nothing.
Making each render faster helps, but some screens are slow because they hold a lot of content, such as a large filtered list or a complex chart. Concurrent rendering does not shorten that work. It changes when React does the work and what happens if something more important arrives.
Two phases: render and commit
React updates the screen in two phases. The split decides what can be interrupted.
- Render phase. React calls your components and works out what the UI should look like. It does not touch the DOM. This phase is pure computation.
- Commit phase. React applies the result to the DOM, then runs layout effects and later
useEffectcallbacks. This phase is synchronous and cannot be interrupted.
In the legacy synchronous mode, both phases run straight through once started. In concurrent rendering, only the render phase can be paused or thrown away. That is safe because the render phase has no visible side effects. Abandoning a half-finished render leaves the screen exactly as it was.
Time slicing and interruption
React represents the work of a render as a tree of small units, one per component. A concurrent render processes these units in a loop. After each unit, React checks whether it has used up its time slice, which is a few milliseconds. If it has, React hands control back to the browser. The browser can then process input and paint. Afterward, React continues from where it stopped.
Between units, React also checks whether a higher-priority update has arrived. If one has, React does not finish the old render. It discards the work in progress, renders the urgent update first, and commits it. Then it starts the low-priority render again, using the latest state.
React decides what counts as urgent. Discrete events such as clicks and key presses get high priority. Updates wrapped in startTransition, and values passed through useDeferredValue, get low priority. React tracks these priorities internally with a system called lanes. You never manage lanes directly, but it helps to know that the priority comes from how an update was started, not from the component that owns the state.
Seeing it in code
The following example makes the effect visible. SlowItem busy-waits for 1 ms, so a list of 200 items takes about 200 ms to render. Each item is its own component. That matters, because React can only pause between components.
import { useState, useTransition } from 'react';
function SlowItem({ text }) {
const start = performance.now();
while (performance.now() - start < 1) {
// Busy-wait to simulate an expensive component.
}
return <li>{text}</li>;
}
function SlowList({ text }) {
const items = [];
for (let i = 0; i < 200; i++) {
items.push(<SlowItem key={i} text={`${text} ${i}`} />);
}
return <ul>{items}</ul>;
}
export default function App() {
const [text, setText] = useState('');
const [listText, setListText] = useState('');
const [concurrent, setConcurrent] = useState(false);
const [isPending, startTransition] = useTransition();
function handleChange(e) {
const value = e.target.value;
setText(value);
if (concurrent) {
startTransition(() => {
setListText(value);
});
} else {
setListText(value);
}
}
return (
<>
<label>
<input
type="checkbox"
checked={concurrent}
onChange={(e) => setConcurrent(e.target.checked)}
/>
Use a transition for the list
</label>
<br />
<input value={text} onChange={handleChange} placeholder="Type quickly" />
{isPending && <p>Updating list...</p>}
<SlowList text={listText} />
</>
);
}Mount this with createRoot (covered in the next chapter) and type quickly in each mode.
- Checkbox off. Every keystroke updates the input and the list in one urgent render. Each render takes about 200 ms, so characters appear late and in bursts.
- Checkbox on. The keystroke updates
texturgently, so the input stays responsive. The list update is a transition. If you type again before the list finishes, React drops the unfinished list render and starts over with the newer text. The list may skip intermediate values and only show the last one.
The total list work did not shrink. React only reordered it, so the input is never waiting behind it.
Render may run without a commit
Interruptible rendering has a consequence for how you write components. React may call a component and then throw away the result. A render does not guarantee a commit. A component can also be called more than once for the same update. This has always been allowed, and React Strict Mode in development double-invokes components to expose problems early. Concurrent rendering makes the case routine.
So rendering must be pure. The same props, state and context must produce the same output, and the render must not change anything outside the component.
let renderCount = 0;
function Counter({ label }) {
renderCount += 1; // Impure: mutates a module variable during render.
return (
<p>
{label}: rendered {renderCount} times
</p>
);
}This component shows a number that does not match what users see. React may render it several times for one visible update. Put side effects such as logging to a server, subscriptions, or timers in event handlers or useEffect. Both run only for committed work.
Keeping external data consistent
Interruptible rendering also raises a consistency question. A render can span several time slices. If it reads data from outside React, such as a mutable global store, that data could change between slices. Then different parts of the same render would show different values. This is called tearing.
State and context from React are safe, because React controls when they change during a render. For external stores, use useSyncExternalStore. It lets React detect changes during a render and keep the whole tree consistent. Most store libraries already use it internally.
What concurrent rendering does not do
Several limits are easy to overlook:
- It does not run on another thread. Rendering still happens on the main thread. React only schedules it in smaller pieces.
- It does not reduce total work. Restarting a render can mean more total computation than before. The gain is responsiveness, not throughput.
- It cannot split one slow component. React yields between components, never in the middle of one. If a single component takes 300 ms, the page is blocked for 300 ms no matter what.
- It does not interrupt the commit phase. A large DOM update still happens in one go.
- It only applies to marked updates. An update is interruptible only if it is a transition or a deferred value. Ordinary updates still render at urgent priority, with no benefit from interruption.
Synchronous and concurrent rendering compared
| Aspect | Synchronous render | Concurrent (transition) render |
|---|---|---|
| Can be paused | No | Yes, between components |
| Can be abandoned | No | Yes, if a higher-priority update arrives |
| Component may be called without a commit | Rarely | Routinely |
| Input during a slow render | Blocked until it ends | Handled first |
| Total work | Fixed | Equal or higher |
| Commit phase | Synchronous | Synchronous |
Summary of the working model
An update starts with a priority. Urgent updates render and commit without yielding. Transition and deferred updates render in slices. Between slices, React lets the browser handle input, and it checks for more urgent work. If new urgent work arrives, React discards the stale low-priority render and starts it again with current state. Only a finished render is committed to the DOM, all at once.
The next chapters build on this model. First comes opting in with createRoot. After that, startTransition, useTransition and useDeferredValue become tools for telling React which updates may be paused.
Exercise
In the example above, a user types "abc" quickly with the transition checkbox on. Which renders of SlowList are likely to be committed, and why does the input stay responsive?
Show answer
Typically only the final render, for "abc", is committed, and perhaps one or two intermediate ones if the user paused. Each keystroke sets text urgently, so the input renders and commits immediately. The list update is a transition. When a new keystroke arrives, React abandons the unfinished list render and restarts it with the newest value. The input stays responsive because urgent renders are never queued behind the 200 ms list render.
Exercise
A component takes 400 ms to render by itself, with no child components doing the work. You wrap its state update in startTransition. Will the page still freeze, and why?
Show answer
Yes. React can pause only between components, not inside one. The 400 ms component runs to completion once it starts, so the main thread is blocked for that time. The transition only helps if the work can be split across several components or reduced, for example with memoization or less computation.
Exercise
Why must components be pure for concurrent rendering to be safe? Give an example of what could go wrong if one is not.
Show answer
React may call a component and then discard the result, or call it more than once for one visible update. If a component changes something outside itself during render, such as incrementing a module-level counter or sending a request, that change happens for renders that never reach the screen. The counter would overcount and the request would be sent repeatedly. Side effects belong in event handlers or useEffect, which run only for committed work.