Sub topic 12
Async Transitions in React 19
This chapter assumes you know useState, function components, and the basics of useTransition and startTransition from earlier chapters. It covers what changes in React 19, where the new behavior stops, and the mistakes people make most often.
What changes in React 19
In React 18, the function you pass to startTransition had to be synchronous. React ran it immediately and marked every state update made inside it as a transition. If you passed an async function, React did not wait for it. Only the updates made before the first await counted as part of the transition.
React 19 allows startTransition to take an async function. React keeps isPending set to true from the moment you call startTransition until the returned promise settles. That makes isPending useful for a whole sequence such as "send a request, wait for the answer, then update the screen".
| Behavior | React 18 | React 19 |
|---|---|---|
| Callback passed to `startTransition` | Synchronous only | Synchronous or `async` |
| `isPending` is `true` | While the transition render is pending | Until the async function finishes, and then until the resulting render completes |
| State updates after an `await` | Not marked as transitions | Still not marked, unless wrapped in another `startTransition` |
React documentation sometimes calls a function that runs inside a transition an "Action". An Action is only a naming convention for a function you pass to startTransition. It is not a new API.
A worked example
The form below saves a name. While the request runs, the button is disabled and isPending is true.
import { useState, useTransition } from 'react';
function NameForm() {
const [name, setName] = useState('');
const [error, setError] = useState(null);
const [isPending, startTransition] = useTransition();
function handleSubmit() {
startTransition(async () => {
const result = await updateName(name);
startTransition(() => {
setError(result.error ?? null);
});
});
}
return (
<>
<input value={name} onChange={(e) => setName(e.target.value)} />
<button onClick={handleSubmit} disabled={isPending}>
Save
</button>
{error && <p>{error}</p>}
</>
);
}Here updateName stands for any async function that you provide. It might call fetch and return an object such as { error: 'Name is too short' }, or an object with no error property on success.
Follow the sequence of events:
- The user clicks Save.
startTransitionruns the async function andisPendingbecomestrue. - The function reaches
await updateName(name)and pauses. The button stays disabled and the input stays responsive, because the typing handler uses a plainsetNamecall, which is an urgent update. - The promise resolves and the function continues.
- The inner
startTransitionmarkssetErroras a transition. React renders the new error state without blocking urgent work. - The async function returns. Once the work finishes,
isPendingbecomesfalse.
Why the second startTransition is needed
React tracks "we are inside a transition" with a global flag that is set only while your callback runs synchronously. When the function reaches an await, it returns control to the caller, and React clears the flag. When the function resumes later, React has no way to know that it belongs to the transition.
So the code after await runs as ordinary code. If it calls setError(...) directly, that update is urgent, not a transition. React will render it right away, and it may do so in a way that can show a loading fallback or block other interactions.
startTransition(async () => {
const result = await updateName(name);
// Not a transition. This update is urgent.
setError(result.error ?? null);
});The fix is the wrapper used in the example above: call startTransition again around every state update that follows an await.
startTransition(async () => {
const result = await updateName(name);
startTransition(() => {
setError(result.error ?? null);
});
});The React team has described this as a known limitation of the current JavaScript runtime. JavaScript has no built-in way to carry context across await boundaries, so React cannot restore the flag for you.
How isPending behaves
With an async transition, isPending covers more time than it did in React 18. It stays true across the network wait, and the same flag then covers the render that follows. You can use one flag to cover both phases.
Two details are worth knowing.
- Several transitions can overlap. React keeps
isPendingset while any transition started from thatuseTransitioncall is still running. isPendingbelongs to theuseTransitioninstance that produced it. A standalonestartTransitionimported fromreacthas no pending flag at all.
What counts as urgent, and what does not
An async transition does not make the network request itself faster or cancel it. It only controls how React schedules the state updates that depend on it. Three consequences follow.
- Urgent updates, such as typing in a controlled input, must stay outside the transition. If you put
setNameinsidestartTransition, the input may lag, because React is allowed to delay that update. - Updates made in the transition can be interrupted by urgent updates. React may throw away a partly finished render and start again.
- Your async function is not interrupted. React can pause a render, but it cannot pause a function that is running your code.
Common pitfalls
Forgetting the wrapper after await
Covered above. If a state update after an await seems to behave like a normal update, check that it is wrapped.
Expecting updates to run in order
Async transitions do not guarantee that a series of overlapping requests finish in the order you started them. If a user clicks Save twice, two functions are in flight. React does not cancel the first. Disable the control while isPending is true, as the example does, or ignore stale results yourself.
import { useRef, useState, useTransition } from 'react';
function Search() {
const [results, setResults] = useState([]);
const [isPending, startTransition] = useTransition();
const latest = useRef(0);
function search(query) {
const id = ++latest.current;
startTransition(async () => {
const data = await fetchResults(query);
if (id !== latest.current) return; // a newer search has started
startTransition(() => {
setResults(data);
});
});
}
return (
<>
<input onChange={(e) => search(e.target.value)} />
{isPending && <p>Searching...</p>}
<ul>
{results.map((r) => (
<li key={r}>{r}</li>
))}
</ul>
</>
);
}In this example fetchResults is again a function you provide. The latest ref holds the number of the most recent search. A response from an older search sees that its id no longer matches and returns without updating state.
Controlled inputs inside a transition
A controlled input must update synchronously. Putting setText(value) inside a transition makes typed characters appear late or revert. Keep the input's own state urgent and put only the expensive derived state in the transition, as in the search example from an earlier chapter.
Throwing errors inside the async function
If the async function throws, isPending becomes false and the error is not caught for you. Within a component, React rethrows the error to the nearest error boundary. If you want to show an error message in the component itself, catch it yourself.
startTransition(async () => {
try {
await updateName(name);
} catch (e) {
startTransition(() => {
setError(e.message);
});
}
});Note that the setError call in the catch block also needs its own startTransition, for the same reason as before.
Using transitions to control text input values
Transitions are for rendering work, not for validating or storing data. They do not add a retry, a timeout or a rollback. If you need an optimistic update that reverts on failure, React 19 provides useOptimistic for that case. It is outside the scope of this chapter.
When to use an async transition
Use one when all of these are true:
- A user action starts an asynchronous operation, such as a form submission.
- The screen should show a pending state until the operation and the resulting render are done.
- The updates that follow the operation are not urgent.
Do not use one when you only need to defer an expensive render and no async work is involved. A plain synchronous startTransition or useDeferredValue is simpler in that case.
Summary of rules
| Situation | What to do |
|---|---|
| Need a pending flag for async work | Use `useTransition` and pass an async function |
| State update after an `await` | Wrap it in another `startTransition` |
| Controlled input value | Keep it outside the transition |
| Overlapping requests | Disable the control or discard stale results |
| Error thrown in the async function | Catch it, or let an error boundary handle it |
Exercise
A component calls startTransition(async () => { const data = await load(); setItems(data); }). The setItems call re-renders a large list and sometimes blocks typing in a nearby input. What is wrong, and how do you fix it?
Show answer
The setItems call runs after an await, so React no longer treats it as part of the transition. It is an urgent update, and the large render can block typing. Wrap it in a second transition: startTransition(() => { setItems(data); }) inside the async function, after the await.
Exercise
In the Save form from this chapter, why is the button disabled with disabled={isPending} rather than leaving it enabled?
Show answer
In React 19, isPending stays true for the full duration of the async function. Disabling the button prevents a second click from starting an overlapping request while the first is still running. React does not cancel or reorder overlapping async transitions, so without this the results could arrive out of order.