Sub topic 1
Async transitions and pending state with useTransition
Prerequisites
This chapter assumes you know React function components, useState, and useEffect. You should also be comfortable with async functions, promises, and fetch. You do not need to know about forms or Server Functions yet. Later chapters cover them.
What changed in React 19
Before React 19, startTransition accepted only a synchronous function. It marked state updates as non-urgent, so React could keep the page responsive while it rendered them. It could not wait on a network request.
React 19 lets you pass an async function to startTransition. React then tracks the whole function, including each await, as one unit of work. While that work runs, the isPending value from useTransition stays true. When the function finishes, isPending becomes false.
This gives you a pending state without writing const [loading, setLoading] = useState(false) and the matching try/finally code.
The useTransition hook
useTransition returns an array of two values:
isPendingis a boolean. It istruewhile a transition is running.startTransitionis a function. You call it with the work you want React to track.
const [isPending, startTransition] = useTransition();Call useTransition at the top level of a component, like any other hook.
A worked example
The component below sends a new name to a server. It disables the button while the request runs and shows an error if the request fails.
import { useState, useTransition } from "react";
async function updateName(name) {
const response = await fetch("/api/name", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name }),
});
if (!response.ok) {
return "Could not update the name.";
}
return null;
}
export function UpdateName() {
const [name, setName] = useState("");
const [error, setError] = useState(null);
const [isPending, startTransition] = useTransition();
function handleSubmit() {
startTransition(async () => {
const message = await updateName(name);
if (message) {
setError(message);
return;
}
setError(null);
});
}
return (
<div>
<input value={name} onChange={(e) => setName(e.target.value)} />
<button onClick={handleSubmit} disabled={isPending}>
Update
</button>
{error && <p>{error}</p>}
</div>
);
}Follow what happens when the user clicks the button:
handleSubmitcallsstartTransitionwith an async function.- React sets
isPendingtotrueright away, and the button becomes disabled. - The function awaits
updateName. The component stays interactive during the request. - The request finishes. The function calls
setErrorwith either a message ornull. - React sets
isPendingback tofalse.
Why this is the start of Actions
React calls a function that runs inside a transition an Action. The async function you passed to startTransition is an Action. The next chapter defines Actions more fully. For now, notice what React manages for you: the pending flag, and ordering of the state updates the Action makes.
Common errors
State updates after an await are not tracked
React tracks the async function, but only state updates made in the synchronous part of it are marked as transition updates. In the current React 19 release, a setState call that comes after an await is not automatically part of the transition. If you need it to be, wrap it in another startTransition.
startTransition(async () => {
const message = await updateName(name);
startTransition(() => {
setError(message);
});
});For a simple error message like the one above, this rarely matters. It matters when the update triggers an expensive render that you want React to keep interruptible.
Using transitions for controlled text input
Do not wrap the setName call in onChange in a transition. A transition update can be delayed, and a controlled input must update immediately, or the text will lag or jump.
Forgetting to handle failures
If the awaited function throws, the error is not stored anywhere for you to read in the component. The example above returns an error message instead of throwing. Treat each failure path explicitly. A later chapter covers error handling in depth.
Calling useTransition conditionally
Like all hooks, useTransition cannot sit inside a condition or loop. Move the condition inside the function you pass to startTransition.
Quick checklist
- Import
useTransitionfromreact. - Pass an
asyncfunction tostartTransition. - Read
isPendingto disable controls or show a spinner. - Return or store error information yourself.
- Keep controlled input updates outside transitions.
Exercise
In the UpdateName component, the user clicks Update and the request takes three seconds. What is the value of isPending during those three seconds, and what would you change to show the text "Saving..." on the button instead of "Update"?
Show answer
isPending is true for the whole three seconds, from the moment startTransition is called until the async function finishes. Change the button content to {isPending ? "Saving..." : "Update"}.
Exercise
Why should you not put the setName call from a controlled input's onChange handler inside startTransition?
Show answer
Transition updates can be delayed or interrupted. A controlled input must reflect each keystroke immediately, so delaying its state update makes the typed text lag or jump.