Sub topic 4
How forms reset after an Action
This chapter assumes you know uncontrolled inputs, FormData, and the <form action={fn}> pattern from the previous chapter. It covers what React does to the fields when an Action finishes, and how to work with that behavior.
What React does for you
In React 19, passing a function to a form's action prop makes React run that function as an Action. When the Action completes, React resets the form. This is the same effect as calling form.reset() in the browser: each uncontrolled field returns to its defaultValue, defaultChecked, or defaultSelected.
export function NoteForm({ addNote }) {
async function submit(formData) {
await addNote(formData.get("note"));
}
return (
<form action={submit}>
<input name="note" />
<button type="submit">Add note</button>
</form>
);
}You write no reset code here. After addNote resolves, the note field is empty again. Before React 19, you would have called event.preventDefault() and cleared the field yourself.
Two details matter:
- The reset happens after the Action finishes, not when the form is submitted. While an async Action is pending, the user's typed text stays in the field.
- The reset only applies to forms whose
actionis a function. A form withonSubmitandpreventDefaultis not reset by React.
Reset also happens after a validation error
React does not check whether the submission "worked". If your Action returns an error message instead of throwing, the form still resets. The user then loses what they typed.
That is a problem with a form like this one:
import { useActionState } from "react";
async function register(previousState, formData) {
const email = String(formData.get("email") ?? "").trim();
if (!email.includes("@")) {
return { error: "Enter a valid email address.", email };
}
return { error: null, email: "" };
}
export function Register() {
const [state, formAction, isPending] = useActionState(register, {
error: null,
email: "",
});
return (
<form action={formAction}>
<input name="email" defaultValue={state.email} />
<button type="submit" disabled={isPending}>
Register
</button>
{state.error && <p role="alert">{state.error}</p>}
</form>
);
}The fix is to return the submitted value in the state and use it as defaultValue. React resets the field to its default, and the default is now the user's own text. On success the Action returns email: "", so the field ends up empty. This is why the useActionState example in this tutorial returns name in every branch.
Controlled inputs
The automatic reset targets the DOM's own field values. A controlled input, one with value and onChange backed by useState, shows whatever its state holds. After the reset, React renders the state value again, so the field is not cleared. If you use a controlled input, clear it yourself:
import { useState } from "react";
export function ControlledNote({ addNote }) {
const [note, setNote] = useState("");
async function submit() {
await addNote(note);
setNote("");
}
return (
<form action={submit}>
<input name="note" value={note} onChange={(e) => setNote(e.target.value)} />
<button type="submit">Add note</button>
</form>
);
}For simple forms, uncontrolled inputs with an Action are less code and get the reset for free.
Resetting early or manually
Sometimes you want the field cleared immediately, before a slow request returns. The optimistic example earlier in this tutorial calls formRef.current.reset() inside the Action for this reason. The message appears in the list as "Sending...", and the input is ready for the next message.
If you submit through onSubmit and a transition rather than the action prop, React will not reset the form. You can ask for the same reset with requestFormReset from react-dom. Call it inside a transition. React then performs the reset when that transition finishes.
import { startTransition } from "react";
import { requestFormReset } from "react-dom";
export function ManualForm({ save }) {
function handleSubmit(event) {
event.preventDefault();
const form = event.currentTarget;
const formData = new FormData(form);
startTransition(async () => {
requestFormReset(form);
await save(formData.get("title"));
});
}
return (
<form onSubmit={handleSubmit}>
<input name="title" />
<button type="submit">Save</button>
</form>
);
}Common mistakes
- Expecting a controlled input to clear. Reset the state instead.
- Returning an error without returning the submitted values, so the user's text disappears.
- Adding your own reset call and also relying on the automatic one. This is harmless but redundant, unless you need the early clear.
Exercise
A signup form uses useActionState. The Action returns { error: "Email taken" } when validation fails, and users complain that the email field is empty after the error appears. Why does this happen, and what is the fix?
Show answer
React resets uncontrolled fields to their defaultValue after the Action finishes, even when the Action returns an error. The input has no default, so it empties. Return the submitted email in the state (for example { error: "Email taken", email }) and set defaultValue={state.email} on the input. The reset then restores the user's text.