Sub topic 2
Actions and transitions: the context useOptimistic works in
This chapter assumes you know function components, useState, props, and async/await. It explains the React 19 idea of an Action, because useOptimistic only behaves correctly inside one.
What an Action is
In React 19, an Action is a function that runs inside a transition and may be async. A transition is a state update that React marks as non-urgent, so the UI stays responsive while the update is processed.
React 19 lets a transition callback be async. While that callback is pending, React tracks the whole operation as one unit. It knows when the operation starts, and it knows when the awaited work has finished or failed.
That lifetime is what useOptimistic depends on. The optimistic value exists for as long as the Action is pending. When the Action ends, React drops the optimistic value and shows the real state again.
Starting an Action with startTransition
You can start an Action yourself with startTransition and an async function:
import { startTransition, useState } from "react";
function SaveButton({ save }) {
const [saved, setSaved] = useState(false);
function handleClick() {
startTransition(async () => {
await save();
startTransition(() => {
setSaved(true);
});
});
}
return <button onClick={handleClick}>{saved ? "Saved" : "Save"}</button>;
}The setSaved(true) call after the await is wrapped in its own startTransition. React does not keep track of the transition once the code resumes after an await. A state update made there is treated as an ordinary update, not as part of the Action.
Form actions are Actions too
Passing a function to a form's action prop makes that function an Action. React runs it inside a transition for you, so you do not call startTransition.
function MessageForm({ sendMessage }) {
async function formAction(formData) {
await sendMessage(formData.get("message"));
}
return (
<form action={formAction}>
<input type="text" name="message" />
<button type="submit">Send</button>
</form>
);
}React passes the form's FormData to the function and resets uncontrolled fields when the Action completes successfully. A later chapter covers form actions in more detail.
Where useOptimistic fits
Call the setter returned by useOptimistic only from inside an Action. Two places qualify:
- A function passed to a form's
actionprop. - An async function passed to
startTransition.
The next chapter introduces the hook. For now, note the shape of the lifecycle.
Common errors
- Calling the setter outside an Action. If you call it from a plain event handler or an effect, React warns that an optimistic update happened outside a transition or Action. The optimistic value does not behave as intended, so wrap the call in
startTransitionor move it into a form action. - Putting a state update after
awaitwithout its own transition. Updates after anawaitare no longer part of the Action. If they need transition behavior, wrap them in anotherstartTransition. - Expecting the optimistic value to outlive the Action. It does not. If the real state has not changed by the time the Action ends, the optimistic change disappears.
Summary
An Action is an async function that React runs in a transition. React treats the whole pending period as one unit. Form action functions and startTransition(async () => ...) both create Actions. useOptimistic is designed for exactly this period: it shows a temporary value while the Action is pending and returns to real state when the Action ends.
Exercise
A developer calls the setter from useOptimistic inside an onClick handler, with no startTransition and no form action. What goes wrong, and how do they fix it?
Show answer
The call is not inside an Action, so React warns and the optimistic value is not tied to a pending Action. The fix is to wrap the work in startTransition(async () => { setOptimistic(value); await save(); }), or to move the logic into a form's action function.
Exercise
Inside startTransition(async () => { await save(); setSaved(true); }), why might setSaved(true) not be treated as part of the transition, and what is the fix?
Show answer
React loses track of the transition after the await, so the update after it is an ordinary update. Wrap it in its own startTransition(() => { setSaved(true); }).