Handling forms in React 19
React 19 came out on December 5, and the headline feature is Actions.
React 19 came out on December 5, and the headline feature is Actions. For forms, that means much of the usual boilerplate goes away: the isSubmitting state, the error state, preventDefault(), and controlled inputs for fields you only read on submit.
A function as the form action
You can now pass a function to a form’s action:
function Newsletter() {
async function subscribe(formData) {
await fetch('/api/subscribe', { method: 'POST', body: formData });
}
return (
<form action={subscribe}>
<input name="email" type="email" required />
<button>Subscribe</button>
</form>
);
}
React calls subscribe with the form’s FormData when it’s submitted. No onSubmit, no preventDefault(), no useState per field. After the action finishes successfully, React also resets the form’s uncontrolled fields for you.
The browser’s built-in validation, like required and type=“email”, still runs first.
useActionState: results and errors
Most forms need to show something after submitting: an error, a success message. useActionState wraps an action and keeps its last result as state:
import { useActionState } from 'react';
async function updateProfile(prevState, formData) {
const name = String(formData.get('name') ?? '').trim();
const email = String(formData.get('email') ?? '').trim();
if (!name) return { error: 'Name is required', savedAt: null };
try {
await saveProfile({ name, email });
return { error: null, savedAt: Date.now() };
} catch {
return { error: 'Could not save. Try again.', savedAt: prevState.savedAt };
}
}
function ProfileForm() {
const [state, formAction, isPending] = useActionState(updateProfile, {
error: null,
savedAt: null,
});
return (
<form action={formAction}>
<input name="name" placeholder="Name" />
<input name="email" type="email" placeholder="Email" />
<SubmitButton />
{isPending && <p>Saving...</p>}
{state.error && <p role="alert">{state.error}</p>}
{state.savedAt && <p>Saved.</p>}
</form>
);
}
A few things are different from a normal event handler:
- The action gets the previous state as its first argument, and FormData as the second.
- Whatever it returns becomes the new state.
- isPending is true while it runs. You don’t manage that yourself.
Notice that errors are returned, not thrown. A thrown error goes to the nearest error boundary, which is right for a bug, but not for “the name is empty”. Return expected errors as state.
If you saw this hook in the release candidate as useFormState from react-dom, it has been renamed and moved to react.
useFormStatus for the submit button
A submit button usually needs to know if its form is pending. Passing isPending down works, but React 19 has a hook for it:
import { useFormStatus } from 'react-dom';
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" disabled={pending}>
{pending ? 'Saving...' : 'Save'}
</button>
);
}
The catch: useFormStatus reads the status of the form it’s rendered inside. Calling it in the same component that renders the