Sub topic 13
Concurrent Rendering, Transitions, and Interruptible Updates
This chapter assumes you know the render phase and commit phase, how useState triggers re-renders, and that rendering must be a pure calculation. It builds on the earlier chapters on batching and Fiber. Here you will see how React can pause or abandon a render, how to mark an update as non-urgent, and what that means for the code you write.
The problem: one long render blocks everything
In the model from earlier chapters, a render runs from start to finish. If a component tree is expensive to render, the browser's main thread is busy for the whole time. The page cannot respond to typing or clicks until the render and commit finish.
Consider a search box that filters 20,000 rows on every keystroke. If each render takes 200 ms, the input feels stuck. The input's own update is cheap, but it has to wait behind the slow list.
Concurrent rendering addresses this. React treats some updates as urgent (typing, clicking, pressing) and others as transitions (the results of that typing). It can start rendering a transition, pause to handle an urgent update, and then resume or restart the transition.
What "concurrent" means here
Concurrent rendering does not mean multiple threads. JavaScript still runs on one thread. It means React can have more than one version of the UI in progress and can interleave work on them.
Two facts from earlier chapters make this possible:
- The render phase has no side effects on the DOM. Because nothing visible changes during render, React can throw away a half-finished render safely.
- Fiber stores the work as a linked data structure instead of using the call stack. React can stop after any fiber and later continue from that point.
The commit phase is different. It mutates the DOM and runs layout effects synchronously, and it is never interrupted.
Urgent updates and transitions
React assigns each update a priority. Updates from direct interactions, such as setText inside a keypress handler, are urgent by default. An update wrapped in a transition is marked as lower priority.
The useTransition hook returns a flag and a function:
import { useState, useTransition } from 'react';
const ALL_ROWS = Array.from({ length: 20000 }, (_, i) => `Item ${i}`);
function SlowRow({ text }) {
// Simulate an expensive render
const start = performance.now();
while (performance.now() - start < 0.05) {}
return <li>{text}</li>;
}
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); // non-urgent: the slow list may lag behind
});
}
const rows = ALL_ROWS.filter(r => r.includes(query));
return (
<>
<input value={text} onChange={handleChange} />
{isPending && <p>Updating list...</p>}
<ul style={{ opacity: isPending ? 0.5 : 1 }}>
{rows.map(r => (
<SlowRow key={r} text={r} />
))}
</ul>
</>
);
}There are two state variables on purpose. text drives the input and updates urgently. query drives the list and updates inside a transition. When a keystroke arrives while the list is still rendering for the previous query, React drops that unfinished render and starts over with the newer value.
isPending is true from the moment the transition starts until its result is committed. Use it to show a subtle loading cue. The old UI stays on screen during that time, which is usually better than replacing it with a spinner.
Rules for `startTransition`
- The function you pass runs immediately and synchronously. Only state updates made inside it are marked as transitions.
- Updates made after an
awaitinside the callback are not marked, because the transition scope has ended. Set state before awaiting, or start a new transition after. - A transition cannot control a text input's value. An input whose update is delayed will feel broken, so keep the input state urgent as shown above.
- If you do not need
isPending, importstartTransitiondirectly fromreact.
useDeferredValue
Sometimes you do not control the code that sets the state, for example when the value arrives as a prop. useDeferredValue gives you a lagging copy of a value.
import { useDeferredValue, memo } from 'react';
const Results = memo(function Results({ query }) {
const rows = Array.from({ length: 5000 }, (_, i) => `Item ${i}`).filter(r =>
r.includes(query)
);
return (
<ul>
{rows.map(r => (
<li key={r}>{r}</li>
))}
</ul>
);
});
export function SearchResults({ query }) {
const deferredQuery = useDeferredValue(query);
const stale = deferredQuery !== query;
return (
<div style={{ opacity: stale ? 0.5 : 1 }}>
<Results query={deferredQuery} />
</div>
);
}When query changes, React first re-renders with the old deferredQuery, which is fast, and then schedules a background render with the new value. If query changes again, the background render restarts. memo matters here: without it, Results would re-render in the urgent pass even though its props did not change meaningfully.
Both tools do the same job in different places. Use useTransition when you own the setState call. Use useDeferredValue when you only receive the value.
How interruption works in practice
When a transition render starts, React works through fibers in small units and checks periodically whether it should yield to the browser or to a higher-priority update. This is why the input in the example stays responsive: React finishes the urgent render, commits it, and then returns to the transition.
Several consequences follow:
- A render may run more than once before it commits. Any render can be discarded. Your component function may be called and its result never shown.
- A render can be started and never committed. If a newer update makes it obsolete, React drops it.
- Effects only run for committed renders. A discarded render never runs its effects, which is one more reason side effects belong in effects and event handlers, not in the body of a component.
- Urgent updates are never starved by transitions. The reverse is not guaranteed: during continuous urgent updates, a transition can wait. React does eventually let a starved update through, but a transition can lag during sustained input.
Interaction with batching and Suspense
Transitions are batched with other updates of the same priority, following the batching rules from the earlier chapter. Several setState calls inside one startTransition produce one transition render.
Transitions also work with Suspense. If a transition causes a component to suspend, for example while loading data, React keeps showing the current UI instead of falling back to a loading placeholder, and isPending stays true. A Suspense fallback appears only for content that was not on screen before. This is a common reason to prefer a transition for navigation.
Why purity matters more now
Strict Mode in development calls component bodies twice to expose impure code. Concurrent rendering makes that check practical, not theoretical, because React really may render more than once or throw a render away.
This component breaks under concurrent rendering:
let renderCount = 0;
function Badge() {
renderCount += 1; // mutates outside state during render
return <span>Render #{renderCount}</span>;
}The counter increases for discarded renders and double renders, so it does not match what the user sees. Keep render free of mutations to outside variables, and put them in an effect or event handler.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Wrapping the input's own `setState` in a transition | Typed characters appear late or jump | Keep input state urgent and transition only the derived state |
| Expecting `startTransition` to make a slow render faster | The render takes just as long, but no longer blocks urgent updates | Reduce the work itself with memoization, virtualization, or less data |
| Calling `setState` after `await` inside `startTransition` | That update is treated as urgent | Update before the `await`, or open a new transition |
| Reading mutable external data during render | Different parts of one render see different values (tearing) | Use `useSyncExternalStore` for external stores |
| Using a transition for every update | No benefit, extra renders and complexity | Use it only where an update is slow and its delay is acceptable |
Measuring the effect
Profile before and after. In the React DevTools Profiler, a transition render appears as its own commit, and interrupted renders show up as work that was started and replaced. Compare the time the input takes to respond with and without the transition. If the interaction was already fast, the transition adds nothing.
Summary of the model
- Rendering can be interrupted. Committing cannot.
- Updates inside a transition are low priority and can be restarted when newer input arrives.
useTransitionis for updates you trigger.useDeferredValueis for values you receive.- During a transition, React keeps the old UI on screen and reports
isPending. - Pure render functions are what make all of this safe.
Exercise
A search page uses const [query, setQuery] = useState('') and one <input value={query} onChange={e => startTransition(() => setQuery(e.target.value))} />. Users report that typed characters lag and sometimes disappear. Explain the cause and give a fix.
Show answer
The input's value is controlled by query, and that update is marked as a transition, so React may delay or restart it. The input must reflect each keystroke immediately. Use two state variables: keep text as urgent state bound to the input (setText(value)), and set query inside startTransition(() => setQuery(value)) to drive the slow list. Alternatively, keep one urgent state and pass it through useDeferredValue to the list component.
Exercise
A component body increments a module-level variable on each render and displays it. A transition render is started, then abandoned because newer input arrives. What does the variable's value show, and why is this a bug?
Show answer
The variable counts every call to the component function, including the render that was abandoned and never committed, and any Strict Mode double render. The displayed number therefore does not match the number of committed UI updates. Render must be pure, so this kind of mutation belongs in an effect or event handler, where it runs only for committed work.