Sub topic 1
Forms as actions in React 19
Prerequisites
This chapter assumes you know React function components and useState, how HTML forms submit named fields, the FormData API, and how async functions and promises work. You also need React 19 or a framework that ships it.
The problem with hand-managed form state
Before React 19, a typical form kept every input in useState, attached an onSubmit handler, called event.preventDefault(), and then tracked loading, error, and success flags by hand. Each flag was another piece of state to set and reset at the right moment, and it was easy to leave one stale.
React 19 lets a form describe what happens on submit as a single function, called an action. React then tracks the lifecycle of that function for you.
The form `action` prop
In React 19, the action prop of <form> accepts a function as well as a URL string. When the user submits the form, React calls your function and passes it a FormData object that holds the form's named fields.
function SearchForm() {
async function search(formData) {
const query = String(formData.get("query") ?? "");
console.log("Searching for", query);
}
return (
<form action={search}>
<input name="query" type="text" />
<button type="submit">Search</button>
</form>
);
}Notes on this example:
- You do not call
preventDefault(). React prevents the browser's default page navigation for you. - Inputs are read by their
nameattribute. A field with nonameis not inFormData. - The input is uncontrolled, so you do not need
valueandonChangeto read it.
Actions run in transitions
React runs a form action inside a transition. A transition is an update that React treats as non-urgent, so the page stays responsive while it works. While the action is running, React marks it as pending. When the action finishes, React applies any state updates it made together.
This is why an action can be async. React waits for the returned promise and keeps the pending state active until the promise settles. You will read that pending state through isPending in useActionState and through useFormStatus, both covered in later chapters.
What happens on submit
The last step has a consequence you should know now. After a function action succeeds, React resets uncontrolled fields in the form to their default values. A later chapter shows how to keep what the user typed.
Where `useActionState` fits
A plain action function has a gap. It receives the submitted data, but it has nowhere to put a result such as a validation message. You could store the result in useState, which brings back the hand-managed flags.
useActionState closes that gap. It wraps your action and gives you three things: the state returned by the last run of the action, a new action to pass to <form action>, and a pending flag. The next chapter covers its arguments and return values in detail. As a preview:
import { useActionState } from "react";
async function rename(prevState, formData) {
const name = String(formData.get("name") ?? "").trim();
if (!name) return { error: "Name is required." };
return { error: null };
}
export function RenameForm() {
const [state, formAction, isPending] = useActionState(rename, { error: null });
return (
<form action={formAction}>
<input name="name" type="text" />
<button type="submit" disabled={isPending}>Rename</button>
{state.error && <p role="alert">{state.error}</p>}
</form>
);
}Compare this with the first example. The action now takes prevState as its first argument and formData as its second. Whatever the action returns becomes the new state.
Common errors at this stage
- Missing
nameattributes.formData.get("email")returnsnullif the input has noname="email". - Using
onSubmitandactiontogether. Pick one. Theactionprop is the path that React tracks as a transition. - Expecting typed values to survive. Uncontrolled inputs reset after a successful action, so the form appears to clear itself.
Exercise
A form uses <form action={save}> where save is a plain async function. What does React pass to save, and what do you no longer need to call inside it?
Show answer
React passes a FormData object containing the form's named fields. You no longer need to call event.preventDefault(), because React prevents the default browser navigation when the action is a function.
Exercise
After a form action finishes, the text the user typed into an uncontrolled input disappears. Why does this happen?
Show answer
React resets uncontrolled fields in a form to their default values once a function action completes successfully. Keeping the typed value requires returning it in state and using defaultValue, which a later chapter covers.