Sub topic 3
The useActionState hook: arguments and return values
This chapter assumes you know React function components, useState, the HTML FormData API, and async functions. It also assumes you are using React 19. Here you will learn the hook's three arguments, its three return values, and how they connect to a form.
The problem it solves
With useState, a form usually needs separate state for the result, the error, and the loading flag. You write a submit handler that sets each one by hand. useActionState replaces that bookkeeping. It keeps one piece of state, and that state is always the result of the last submission.
The call
const [state, formAction, isPending] = useActionState(action, initialState, permalink);The arguments:
| Argument | Required | Meaning |
|---|---|---|
| `action` | Yes | A function that runs on each submission and returns the next state. |
| `initialState` | Yes | The state before any submission. It can be any value. |
| `permalink` | No | A URL used with server functions so a form can work before client JavaScript loads. |
The return values:
| Value | Meaning |
|---|---|
| `state` | The current state. On the first render it equals `initialState`. After that it is whatever the last action returned. |
| `formAction` | A new function to pass to the form's `action` prop. |
| `isPending` | `true` while an action is running. |
Your original action is not what you give to the form. You give the form formAction, which wraps it. React calls your function for you and handles the state update.
What the action receives
When the form submits, React calls your action with two arguments. The first is the previous state. The second is the submitted FormData. Whatever you return becomes the new state.
import { useActionState } from "react";
async function subscribe(prevState, formData) {
const email = String(formData.get("email") ?? "");
if (!email.includes("@")) {
return { ok: false, error: "Enter a valid email address.", email };
}
await fetch("/api/subscribe", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email }),
});
return { ok: true, error: null, email: "" };
}
export function SubscribeForm() {
const [state, formAction, isPending] = useActionState(subscribe, {
ok: false,
error: null,
email: "",
});
return (
<form action={formAction}>
<input name="email" type="text" defaultValue={state.email} />
<button type="submit" disabled={isPending}>
{isPending ? "Subscribing..." : "Subscribe"}
</button>
{state.error && <p role="alert">{state.error}</p>}
{state.ok && <p>Thanks for subscribing.</p>}
</form>
);
}Notice that prevState comes first, before formData. This is the main difference from a plain form action, which takes only formData. If you forget the extra parameter, formData will hold the previous state and formData.get will fail.
Data flow
Common mistakes
- Passing
actionto the form instead offormAction. The state will never update. - Returning nothing. An action that returns
undefinedsets the state toundefined, andstate.errorthen throws. Always return a value of the same shape asinitialState. - Mutating
prevState. Build a new value, as in[...prevItems, name], rather than changing the old one.
Exercise
A form uses useActionState(save, null). The save function is declared as async function save(formData) { ... }, and formData.get("title") throws an error on submit. What is wrong, and how do you fix it?
Show answer
React passes the previous state as the first argument, so formData actually holds null (the previous state). Change the signature to async function save(prevState, formData). Also pass the formAction returned by the hook to the form, not save itself.