Sub topic 2
How actions run in transitions
This chapter assumes you know useState, async/await, and the basics of useTransition. It explains the mechanism under useActionState: every action runs inside a transition. Knowing this explains where isPending comes from. It also explains why the dispatcher needs startTransition outside a form, and why some state updates after an await behave oddly.
What "action" means in React 19
In React 19, an action is a function that React runs inside a transition. A transition marks the state updates it causes as non-urgent, so React can render them in the background without blocking typing, clicking, or scrolling.
Before React 19, you could only pass a synchronous function to startTransition. React 19 also accepts an async function. React then tracks the promise the function returns. The transition counts as pending from the moment you start it until the promise settles and React has rendered the updates that came out of it.
Three entry points start an action:
| Entry point | How the transition starts |
|---|---|
| `<form action={fn}>` | React wraps the call to `fn` in a transition when the form is submitted. |
| `startTransition(async () => { ... })` | You start the transition yourself. |
| The dispatcher from `useActionState` | Inside a form action, it is already in a transition. Anywhere else, you wrap the call in `startTransition`. |
Because all three use the same mechanism, they share the same pending behavior, the same error handling, and the same interruptible rendering.
What the transition gives you
Running inside a transition has four consequences.
- A pending flag.
useTransitionreturnsisPending.useActionStatereturns its ownisPendingas the third element.useFormStatusreportspendingfor the enclosing form. All of them are true while the action's promise is unsettled. - A responsive UI. The old UI stays on screen while the action runs. React does not replace it with a Suspense fallback for the updates inside the transition.
- Interruptible rendering. If an urgent update arrives, such as a keystroke, React handles it first and resumes the transition render afterwards.
- Error propagation. If the action throws or its promise rejects, React rethrows the error during rendering. The nearest error boundary catches it.
The following diagram shows the order of events for one form submission.
Starting a transition by hand
You can see the mechanism without useActionState. This component saves a name when you click the button. isPending comes straight from useTransition.
import { useState, useTransition } from "react";
function saveName(name) {
return new Promise((resolve) => {
setTimeout(() => resolve(name.trim()), 800);
});
}
export function NameEditor() {
const [name, setName] = useState("");
const [saved, setSaved] = useState("");
const [isPending, startTransition] = useTransition();
function handleClick() {
startTransition(async () => {
const result = await saveName(name);
startTransition(() => {
setSaved(result);
});
});
}
return (
<div>
<input value={name} onChange={(e) => setName(e.target.value)} />
<button onClick={handleClick} disabled={isPending}>
{isPending ? "Saving..." : "Save"}
</button>
<p>Saved name: {saved || "none yet"}</p>
</div>
);
}While the 800 ms promise is unsettled, isPending is true and the button is disabled. The input stays responsive. Its value is a normal urgent update, not part of the transition.
Note the second startTransition around setSaved. The next section explains why it is there.
The update-after-await limitation
React tracks which updates belong to a transition by watching for setState calls that happen synchronously while the function runs. That works up to the first await. After the await, the original call has returned, and React no longer knows the code is part of the transition. A setState call there is treated as an ordinary urgent update.
Two things can go wrong:
- The update renders immediately and does not join the transition's batch. The UI can briefly show the new data while
isPendingis stilltrue. - If several updates come after an
await, they are not guaranteed to land in the same render as the rest of the transition.
The fix is to wrap each state update that follows an await in its own startTransition, as in NameEditor. React's own documentation lists this as a known limitation of async transitions.
The same rule applies to a plain function passed to a form's action prop:
import { startTransition, useState } from "react";
export function TitleForm() {
const [title, setTitle] = useState("Untitled");
async function updateTitle(formData) {
const next = String(formData.get("title") ?? "").trim();
await new Promise((resolve) => setTimeout(resolve, 600));
startTransition(() => {
setTitle(next || "Untitled");
});
}
return (
<form action={updateTitle}>
<input name="title" type="text" defaultValue={title} />
<button type="submit">Rename</button>
<p>Current title: {title}</p>
</form>
);
}Here React wraps the call to updateTitle in a transition for you. The setTitle call after the await still needs its own startTransition.
Where `useActionState` fits
useActionState combines three things around your action function:
- It stores the action's last returned value as state.
- It exposes
isPending, which is true while the action runs in its transition. - It returns a dispatcher that starts the action.
When the dispatcher is passed to <form action={...}>, the form submission supplies the transition. That is why the subscribe form needs no startTransition call. When you call the dispatcher from an onClick handler, nothing has started a transition. You must supply one yourself:
import { startTransition, useActionState } from "react";
async function increment(prevCount, amount) {
return prevCount + amount;
}
export function Counter() {
const [count, dispatch, isPending] = useActionState(increment, 0);
return (
<button
disabled={isPending}
onClick={() => startTransition(() => dispatch(1))}
>
Count: {count}
</button>
);
}Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Console warning that an async function with `useActionState` was called outside a transition | The dispatcher was called from an event handler without `startTransition`. | Wrap the call: `startTransition(() => dispatch(value))`. |
| `isPending` turns `false` but other UI is still stale | A `setState` after an `await` ran as an urgent update. | Wrap it in `startTransition`. |
| The whole page is replaced by an error screen after a failed request | The action threw, and the error reached an error boundary. | Return an error value as state, or catch the error inside the action. |
| A controlled text input lags or ignores typing | The input's `setState` was placed inside a transition. | Keep controlled input updates outside transitions. |
The last row follows from how transitions work. A transition can be postponed, and a text field must update on every keystroke. Keep onChange updates for controlled inputs urgent. Later chapters show how to keep input values with defaultValue and state returned from the action.
Recap of the model
- An action is an async function that React runs inside a transition.
- Form submissions,
startTransition(async ...), and theuseActionStatedispatcher all use this mechanism. - Pending status is true until the promise settles and React renders the result.
- React loses track of the transition after the first
await, so extra state updates after it need their ownstartTransition. - Errors thrown from an action go to an error boundary.
Exercise
A component calls startTransition(async () => { const data = await load(); setItems(data); }). The list sometimes appears on screen while isPending is still true. Why does this happen, 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 renders as an urgent update, which can show the data before the transition finishes. Wrap the update in its own transition: startTransition(() => setItems(data)).
Exercise
You call the dispatcher from useActionState inside an onClick handler as dispatch(1). React logs a warning. What is missing, and why does the same dispatcher work without it when passed to <form action={dispatch}>?
Show answer
The call must be wrapped in startTransition: startTransition(() => dispatch(1)). A form's action prop makes React start a transition for the submission, so the dispatcher is already inside one. An onClick handler does not start a transition, so you must start it yourself.