Sub topic 12
Async transitions in React 19
This chapter assumes you know useTransition, isPending, and how useState updates are batched and rendered. It covers what changes in React 19 when the function you pass to startTransition is async.
What changed
In React 18, the callback passed to startTransition had to be synchronous. It could only call state setters, and React marked those updates as non-urgent. Anything asynchronous, such as a network request, had to happen outside the transition. You tracked its loading state yourself.
In React 19, the callback can be an async function. React keeps isPending set to true for the whole duration of the async work, not just for the render that follows. When the async function finishes and the resulting updates are committed, isPending returns to false.
This gives you a pending state for an action without a separate useState boolean such as isLoading.
A worked example
The form below saves a name. The stub updateName stands in for a real request. It waits one second and rejects empty names.
import { useState, useTransition } from 'react';
async function updateName(name) {
await new Promise((resolve) => setTimeout(resolve, 1000));
return name.trim() ? {} : { error: 'Name is required.' };
}
export default 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>
{isPending && <p>Saving...</p>}
{error && <p>{error}</p>}
</>
);
}Click Save and the button is disabled for about one second. You did not write any code that sets or clears a loading flag.
The rule about updates after await
Look at the inner startTransition call. It is required.
React tracks which updates belong to a transition by running your callback synchronously. Code before the first await runs inside that scope. Code after an await runs later, and React no longer knows it came from the transition. A setError call there is treated as an ordinary urgent update.
Wrapping it in a second startTransition marks it as a transition again. The pending state then lasts until that update has rendered.
Sequence of events
Behavior to know
- Errors. If the async function throws, React rethrows the error during render, so the nearest error boundary catches it. If you want to show an inline message instead, catch the error yourself and set state in a nested
startTransition. - Controlled inputs. Keep the input's own state outside the transition. In the example,
setNameruns directly inonChange. Putting it in a transition would make typing lag. - Several transitions at once. Async transitions that overlap are currently batched together.
isPendingstaystrueuntil all of them finish, and their updates may not render in the order you started them. If order matters, disable the trigger whileisPendingistrue, as the button above does. - Standalone
startTransition. The function imported fromreactaccepts async callbacks too. It gives you noisPending.
Common mistakes
- Setting state after an
awaitwithout a nestedstartTransition, then wondering why the update was urgent. - Treating
isPendingas "the request is in flight" and forgetting it also covers the final render. - Reading the result of the transition from state in the same callback. State is a snapshot, so use the value the async function returned.
Exercise
In an async transition, why does setError(result.error) after await updateName(name) need its own startTransition, and what happens if you leave it out?
Show answer
React identifies transition updates by running the callback synchronously, so only updates before the first await are marked as transitions. An update after the await is treated as urgent. If you leave out the nested startTransition, setError still runs, but as a normal update that can show up while the transition is still pending and does not count toward isPending.
Exercise
Your Save button must not allow a second click while the first save is running. How do you do this with an async transition, and why is it safer than relying on overlapping transitions?
Show answer
Set disabled={isPending} on the button. isPending stays true until the async work and the final update are committed. Overlapping async transitions are batched and may finish out of order, so preventing a second submission avoids unordered results.