Sub topic 2
What an Action is in React 19
This chapter assumes you know function components, useState, and async/await. It also assumes you have seen useTransition and its isPending flag. If not, review them first. Here you will build the model that the rest of the tutorial relies on.
The definition
In React 19, an Action is a function that React runs inside a transition, usually an async one. "Action" is a convention, not a new API. You can call any function that you pass to startTransition an Action. The same word also covers functions you pass to a form's action prop, which later chapters cover.
The change from earlier React is that startTransition now accepts an async function. React tracks that function until its promise settles. While it is in flight, React keeps the transition pending. Before React 19, you wrote the pending flag, the error state, and the ordering logic yourself with several useState calls.
What React manages for you
When you run a mutation as an Action, React takes over four jobs:
| Concern | What React does |
|---|---|
| Pending state | `isPending` is `true` from the start of the Action until the async work finishes. |
| Errors | If the Action throws, React propagates the error to the nearest error boundary. |
| Optimistic updates | Temporary state from `useOptimistic` shows while the Action runs, then reverts to real state. |
| Forms | A function passed to `<form action>` runs as an Action, so the form can reflect its status. |
This chapter focuses on pending state and errors. Optimistic updates and forms each get their own chapters.
How an Action runs
The UI stays responsive during the wait. Because the work runs in a transition, React can keep showing the current screen and handle other input.
A worked example
This like button runs its request as an Action.
import { useState, useTransition } from "react";
export function LikeButton({ postId, initialLikes }) {
const [likes, setLikes] = useState(initialLikes);
const [isPending, startTransition] = useTransition();
function handleClick() {
startTransition(async () => {
const response = await fetch(`/api/posts/${postId}/like`, {
method: "POST",
});
if (!response.ok) {
throw new Error("Like failed");
}
const data = await response.json();
startTransition(() => {
setLikes(data.likes);
});
});
}
return (
<button onClick={handleClick} disabled={isPending}>
{isPending ? "Liking..." : `Like (${likes})`}
</button>
);
}Three details matter here:
isPendingcomes fromuseTransition. You never callsetIsPending.- Throwing inside the Action is a supported way to report failure. With no
try/catch, React sends the error to an error boundary above this component. setLikessits inside a secondstartTransition. The reason is explained next.
Common mistakes
- Reading
isPendingas "the request failed or finished." It means only that the Action has not settled yet. Track results and errors separately. A later chapter showsuseActionStatefor that. - Calling the async function outside
startTransition. A plainawaitin an event handler gets no pending state from React. Only work inside the transition callback is tracked. - Swallowing errors with an empty
catch. React treats the Action as successful if it returns normally. If you catch an error and do nothing, the pending flag clears and the user sees no sign of the failure. - Expecting
startTransitionto return a promise you can await. Treat it as fire-and-track. Put follow-up work inside the Action itself.
Where this leads
Every later tool in this tutorial builds on this model. <form action> runs a function as an Action. useActionState wraps an Action and keeps its result. useOptimistic only works while an Action is pending. If you can explain what happens between startTransition(async ...) and the settled promise, those hooks are variations on one idea.
Exercise
A developer writes startTransition(async () => { await save(); setSaved(true); }). The pending flag works, but setSaved(true) is not treated as part of the transition. Why, and how do you fix it?
Show answer
The setSaved(true) call runs after an await, so it is outside the synchronous scope React uses to mark updates as transition updates. Fix it by wrapping that call in its own startTransition(() => { setSaved(true); }) inside the Action.
Exercise
Inside an Action, fetch returns a response with ok set to false. Name two ways to report this, and say what happens in each case.
Show answer
One option is to throw an error, which React sends to the nearest error boundary. The other is to catch the problem and store a message in state, such as setError(message), so the component shows it inline. If you do neither, React treats the Action as successful.