Sub topic 13
Concurrent Rendering, Transitions, and Interruptible Updates
This chapter assumes you know the render and commit phases, how React batches state updates, and the basic idea of Fiber from earlier chapters. You should also be comfortable with useState.
The problem: a long render blocks the page
A synchronous render runs from start to finish before the browser can respond to anything else. If one component takes 200 ms to render, typing in an input feels frozen for 200 ms.
Concurrent rendering removes that limit. React can start rendering, pause to let the browser handle input, resume later, or throw the work away. You get this behavior in React 18 and later when you create the root with createRoot.
Why rendering can be interrupted
Two properties from earlier chapters make interruption safe:
- Render is a pure calculation, so abandoning a half-finished render has no side effects.
- Fiber stores the work as many small units. React can stop between units and continue from where it left off.
The commit phase is different. React never interrupts it, because a half-applied DOM update would be visible to the user. The only work that can be paused, restarted, or discarded is the render phase.
Urgent updates and transitions
React treats updates as one of two kinds:
- Urgent: typing, clicking, pressing. The user expects an immediate response.
- Transition: a change that moves the UI to a new result and can arrive a moment later.
You mark an update as a transition with startTransition, or with useTransition, which also tells you whether one is pending.
import { memo, useState, useTransition } from 'react';
const items = Array.from({ length: 20000 }, (_, i) => `Item ${i}`);
const SlowList = memo(function SlowList({ query }) {
const rows = items
.filter(item => item.includes(query))
.map(item => <li key={item}>{item}</li>);
return <ul>{rows}</ul>;
});
export default function Search() {
const [text, setText] = useState('');
const [query, setQuery] = useState('');
const [isPending, startTransition] = useTransition();
function handleChange(e) {
const value = e.target.value;
setText(value); // urgent: keeps the input responsive
startTransition(() => {
setQuery(value); // transition: the slow list can lag behind
});
}
return (
<>
<input value={text} onChange={handleChange} />
{isPending && <p>Updating list...</p>}
<SlowList query={query} />
</>
);
}Both setText and setQuery run in the same event handler, but React gives them different priorities. The input updates first. The list render happens in the background. memo matters here: during the urgent render, query has not changed yet, so SlowList is skipped.
What happens when the user keeps typing
The old list stays on screen until a transition render finishes and commits. React does not show intermediate or stale-mixed results.
Gotchas
- Render can run more than once per commit. A render may start and then be discarded, so render functions must have no side effects. This is the same purity rule that Strict Mode checks.
- Effects only run for committed renders. A discarded render never reaches the commit phase, so its effects never run.
- Call the state setter synchronously inside
startTransition. In React 18, updates scheduled after anawaitare not marked as transitions. - Do not wrap controlled input state in a transition. The input must update urgently, or typing will lag.
useDeferredValueis an alternative. It gives you a lagging copy of a value when you do not control the code that sets it.- A transition does not make rendering faster. It only changes when the work happens and lets newer input win. A slow component is still slow, so measure it with the Profiler.
Exercise
In the Search example, the user types "a", then "ab" before the list for "a" finishes rendering. What happens to the unfinished render for "a", and why is that safe?
Show answer
React discards it and restarts the list render with the query "ab". It is safe because the render phase is pure and has no side effects, and nothing from the abandoned render was committed to the DOM. The old list stays visible until a render finishes and commits.