Sub topic 9
When to Reach for useActionState Instead
This chapter assumes you know React function components and hooks, and that you have built a basic SubmitButton with useFormStatus in the earlier chapters. It also assumes you are on React 19, where useActionState is exported from react. In earlier canary builds the same hook was called useFormState and lived in react-dom.
What useFormStatus cannot do
useFormStatus answers one question: is the parent form submitting right now? It returns pending, data, method, and action. It does not return anything the action produced. It has no place to put a validation error, a success message, or a server response.
It also has a placement rule. It must be called from a component rendered inside the form, so it suits leaf components such as buttons and spinners. The component that owns the form cannot use it for its own form.
useActionState covers what is left.
What useActionState gives you
useActionState wraps an action function and tracks its result. You call it in the component that renders the form:
const [state, formAction, isPending] = useActionState(action, initialState);stateis the value your action last returned, orinitialStatebefore the first submission.formActionis the wrapped function. You pass it to the form'sactionprop.isPendingistruewhile the action runs.
The wrapped action receives the previous state as its first argument and the FormData as its second. That is a different signature from a plain form action, which receives only FormData.
Comparing the two hooks
| Need | useFormStatus | useActionState |
|---|---|---|
| Disable a button while submitting | Yes, from a child component | Yes, with `isPending` |
| Show a value the action returned | No | Yes, through `state` |
| Where it can be called | Only in a child of the form | In the component that owns the form |
| Read the submitted `FormData` while pending | Yes, through `data` | Only inside the action itself |
| Reusable across many forms | Yes, with no props | No, tied to one action |
A decision rule
The diagram shows how to choose.
Example: returning validation errors
This form validates an email on submit and shows the error. The error has to come from the action's return value, so useFormStatus alone would not work.
'use client';
import { useActionState } from 'react';
async function subscribe(previousState, formData) {
const email = String(formData.get('email') ?? '');
await new Promise((resolve) => setTimeout(resolve, 1000));
if (!email.includes('@')) {
return { ok: false, error: 'Enter a valid email address.', email };
}
return { ok: true, error: '', email };
}
export default function SubscribeForm() {
const [state, formAction, isPending] = useActionState(subscribe, {
ok: false,
error: '',
email: '',
});
return (
<form action={formAction}>
<input
name="email"
type="email"
defaultValue={state.email}
aria-invalid={Boolean(state.error)}
/>
<button type="submit" disabled={isPending}>
{isPending ? 'Subscribing...' : 'Subscribe'}
</button>
<p role="status">
{state.error || (state.ok ? 'Subscribed ' + state.email : '')}
</p>
</form>
);
}Two details matter here:
- The action returns errors as values. If it throws, no new state is set and the error goes to the nearest error boundary. Return expected failures, such as validation errors. Throw only for unexpected ones.
- React 19 resets uncontrolled fields after an action finishes. The action returns the submitted
email, and the input uses it asdefaultValue, so the user's text survives a failed submission.
Combining the two hooks
You do not have to pick one. useActionState can own the result while a shared SubmitButton uses useFormStatus. This keeps the button reusable and puts the result logic where the form is.
'use client';
import { useActionState } from 'react';
import { useFormStatus } from 'react-dom';
function SubmitButton({ children }) {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? 'Working...' : children}
</button>
);
}
async function rename(previousState, formData) {
await new Promise((resolve) => setTimeout(resolve, 800));
return { name: String(formData.get('name') ?? '') };
}
export default function RenameForm() {
const [state, formAction] = useActionState(rename, { name: '' });
return (
<form action={formAction}>
<input name="name" defaultValue={state.name} />
<SubmitButton>Rename</SubmitButton>
<p>Current name: {state.name}</p>
</form>
);
}The form component ignores isPending because the child button handles the pending state.
Server Actions and progressive enhancement
useActionState also accepts a Server Action. It takes an optional third argument, a permalink URL. With a permalink, a form rendered on the server can still submit before JavaScript loads. Plain useFormStatus has no equivalent, because it only reads status and never wraps the action.
<br>
Common mistakes
- Writing
async function action(formData)and passing it touseActionState. The function must acceptpreviousStatefirst, orformDatawill be the old state. - Forgetting to return a value on every code path, which leaves
stateasundefined. - Using
useFormStatusin the form owner and wondering whypendingstaysfalse. Move the call into a child, or switch toisPending.
Exercise
A form's action returns { error: 'Name is required' } when validation fails. The page must show this message below the input. Which hook do you need, and where is it called? Does the action's signature change?
Show answer
Use useActionState, called in the component that renders the form. useFormStatus cannot return the action's result. Pass the returned formAction to the form's action prop and render state.error. The action's signature changes to (previousState, formData), and it must return the error object instead of throwing.
Exercise
Your app has twelve forms. Each needs the same disabled "Saving..." button, and only three show server messages. How would you structure the hooks?
Show answer
Build one shared SubmitButton that calls useFormStatus, and use it in all twelve forms. In the three forms that show server messages, also call useActionState in the form component and render state. The other nine forms need no useActionState.