Sub topic 2
How actions run in transitions
This chapter assumes you know useState, async functions, and the basics of HTML forms. It explains the mechanism behind the pending flag that useActionState returns later in this tutorial.
Forms as actions
In React 19, you can pass a function to a form's action prop. React calls it with the submitted FormData when the user submits the form. It also prevents the browser's default page navigation.
React calls that function inside a transition. A transition is an update that React treats as non-urgent. While it runs, the current UI stays visible and the user can keep interacting with the page.
What a transition gives you
A transition tracks work that has started but not finished. React 19 lets that work be an async function. Three things follow from this:
- React marks the transition as pending from the moment it starts until the async function settles.
- The page does not freeze or fall back to a loading placeholder that hides existing content.
- If the function throws, React reports the error to the nearest error boundary.
You can see the same mechanism without a form by using useTransition directly.
import { useState, useTransition } from "react";
export function RenameButton() {
const [name, setName] = useState("Untitled");
const [isPending, startTransition] = useTransition();
function rename() {
startTransition(async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
startTransition(() => setName("Report"));
});
}
return (
<button onClick={rename} disabled={isPending}>
{isPending ? "Renaming..." : name}
</button>
);
}The isPending flag is true for the whole second of waiting. When the async function finishes, it turns false.
The second startTransition
The example calls startTransition twice. React only treats state updates as part of a transition while the async function runs synchronously. After the first await, that tracking is lost. A setName call placed after an await would be an ordinary urgent update.
To keep the update inside the transition, wrap it in another startTransition, as the example does. This is a known limitation of async transitions in React 19.
Form actions and useActionState handle this for you. React runs the action in a transition and applies the state you return as part of that same transition. You do not wrap the return value yourself.
The life of a submission
Where pending state shows up
The same transition feeds every pending signal you will use in this tutorial:
| Source | Where you read it |
|---|---|
| `useTransition` | The `isPending` value it returns |
| `useActionState` | The third value in the returned array |
| `useFormStatus` | The `pending` field, read from a child of the form |
All three reflect the same underlying fact: an action is running inside a transition.
Common errors
- Expecting the form to block the page. Transitions are non-blocking. Users can still type in other fields or click elsewhere. Disable the submit button if you do not want double submissions.
- Updating state after an
awaitwithoutstartTransition. The update runs as urgent. It may flash intermediate UI or ignore the transition's pending state. - Throwing for expected failures. An uncaught error goes to an error boundary and replaces part of your UI. Later chapters show how to return validation errors as state instead.
Exercise
In the RenameButton example, what would change if the inner startTransition(() => setName("Report")) were replaced with a plain setName("Report"), and why?
Show answer
The state update would run after the await, where React no longer tracks it as part of the transition. It would be applied as an urgent update instead. The button would still show the pending state while waiting, but the new name would not be committed together with the end of the transition.