React / 88
Actions: async functions in transitions
React 19 lets a transition run an async function. React tracks the pending state, keeps the current screen usable, and plugs the same idea into form action.
An Action is an async transition
In Suspense and transitions you used startTransition for a plain setTab() call. React calls any function you pass to startTransition an Action, and in React 19 that function can be async.
import { useState, useTransition } from "react";
import { saveName } from "./api.js";
export default function RenameButton({ draft }) {
const [name, setName] = useState("Tasty Bites");
const [isPending, startTransition] = useTransition();
function handleSave() {
startTransition(async () => {
const saved = await saveName(draft);
startTransition(() => setName(saved));
});
}
return (
<button onClick={handleSave} disabled={isPending}>
{isPending ? "Saving…" : `Save (now: ${name})`}
</button>
);
}1 · user clicks Save
startTransition(async () => { … })2 · the Action is running
await saveName(name)
3 · set the result
startTransition(() => setSaved(name))isPending = false
isPending turns true at the first startTransition call and stays true until every running Action has finished and its final state is on screen. If several Actions run at once, React currently batches them, so one isPending covers them all.
An async Action with isPending
Try itsaved name (state)
isPending
Why the second startTransition?
Look at line 11 again. After an await, React can no longer tell that your code is still inside the transition. A plain setName(saved) there would run as a normal, urgent update. Wrapping it in startTransition again keeps it part of the Action. React's docs call this a temporary JavaScript limitation that should go away once JavaScript gets a way to carry context across await.
Form actions
<form> now accepts a function in its action prop. React calls it with a FormData object holding every input that has a name. No onSubmit, no e.preventDefault(), no state per input. (New to FormData? See FormData and form actions first.)
import { startTransition, useState } from "react";
import { saveName } from "./api.js";
export default function RenameForm() {
const [name, setName] = useState("Tasty Bites");
async function renameAction(formData) {
const saved = await saveName(formData.get("name"));
startTransition(() => setName(saved));
}
return (
<form action={renameAction}>
<input name="name" defaultValue={name} />
<button type="submit">Save</button>
</form>
);
}When action is a function, React:
Runs it as an Action
Inside a transition, so it can beasync. The method is always POST, whatever themethodprop says.Shares the pending state
Any component inside the form can read it withuseFormStatus. That's the next topic.Sends errors to an error boundary
If the function throws, the nearest error boundary shows its fallback.Resets the form on success
Uncontrolled fields go back to theirdefaultValue. That's why the input above usesdefaultValue={name}: after the reset it shows the saved name.
A submit button can run a different Action with formAction, for example <button formAction={saveDraft}>Save draft</button> next to the main Publish button.
Don't
Before: wire it all by hand
async function handleSubmit(e) {
e.preventDefault();
setIsSaving(true);
try {
await saveName(new FormData(e.target).get("name"));
} finally {
setIsSaving(false);
}
}Do
Now: let the Action do it
async function renameAction(formData) {
await saveName(formData.get("name"));
}
<form action={renameAction}>…</form>Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
