Skip to content
ClapelloNotes by Barath
ReactCh 16 · 1/6
Core5 min readUpdated

React / 88

Actions: async functions in transitions

TL;DR

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.

RenameButton.jsx
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 () => { … })
React sets isPending = true right away

2 · the Action is running

await saveName(name)

The old screen stays visible and usable. You show a spinner or disable the button using isPending.
the promise resolves

3 · set the result

startTransition(() => setSaved(name))
React commits the new screen

isPending = false

The new name and the end of the pending state appear together, in one render.
One Action from start to finish. isPending turns on at the first startTransition call and turns off only after the last state update from the Action is on screen.

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 it

saved name (state)

Tasty Bites

isPending

false

Click Save: the button disables and the old name stays on screen until the fake server answers. Turn on failure to see the error path.

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.)

RenameForm.jsx
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:

  1. Runs it as an Action

    Inside a transition, so it can be async. The method is always POST, whatever the method prop says.
  2. Shares the pending state

    Any component inside the form can read it with useFormStatus. That's the next topic.
  3. Sends errors to an error boundary

    If the function throws, the nearest error boundary shows its fallback.
  4. Resets the form on success

    Uncontrolled fields go back to their defaultValue. That's why the input above uses defaultValue={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.