Sub topic 1
Form Actions: Passing a Function to the Form's action Prop
Prerequisites
This chapter assumes you know the following:
- React function components, props, and hooks such as
useState. - JSX and how to read it.
- Basic HTML forms, and what
FormDatais. - Async functions and promises.
You also need React 19 or a canary build that supports form actions. The examples use react-dom and plain JavaScript.
What a form action is
In plain HTML, the action attribute of a form is a URL. When the user submits the form, the browser sends the field values to that URL and loads the response.
React 19 lets you pass a function to action instead. That function is a form action. When the user submits the form, React calls your function and passes it a FormData object that holds the form's fields.
export default function NameForm() {
async function greet(formData) {
const name = formData.get('name');
console.log('Hello,', name);
}
return (
<form action={greet}>
<input name="name" placeholder="Name" />
<button type="submit">Greet</button>
</form>
);
}Each field is keyed by its name attribute. An input with no name is not included in FormData, so formData.get(...) returns null for it.
What React does on submit
React handles the submission for you. You don't write an onSubmit handler or call event.preventDefault().
- The user clicks the submit button or presses Enter in a field.
- React stops the browser's default navigation.
- React builds a
FormDataobject from the form's fields. - React calls your function with that object.
- If the function is async, React treats the submission as pending until the returned promise settles.
- After the action finishes, React resets uncontrolled fields in the form.
Step 5 is the reason this chapter matters for the rest of the tutorial. Because React tracks the pending period, it can tell components inside the form that a submission is in progress. The useFormStatus hook, introduced in the next chapter, reads that information.
Comparing action with onSubmit
Both approaches work. They differ in who does the bookkeeping.
| Concern | `onSubmit` handler | `action` function |
|---|---|---|
| Stop page reload | You call `event.preventDefault()` | React does it |
| Read field values | You read state or `new FormData(event.target)` | React passes `FormData` as the argument |
| Track pending state | You keep your own `useState` flag | React tracks it, and child components can read it |
| Reset fields | You reset them yourself | React resets uncontrolled fields |
The action approach removes the loading flag you would otherwise write by hand. That is the main gain for pending UI.
Reading values from FormData
FormData offers a few methods you will use often.
export default function SignupForm() {
async function signup(formData) {
const email = formData.get('email');
const plan = formData.get('plan');
const interests = formData.getAll('interest');
await fetch('/api/signup', {
method: 'POST',
body: JSON.stringify({ email, plan, interests }),
headers: { 'Content-Type': 'application/json' },
});
}
return (
<form action={signup}>
<input name="email" type="email" required />
<select name="plan">
<option value="free">Free</option>
<option value="pro">Pro</option>
</select>
<label>
<input type="checkbox" name="interest" value="news" /> News
</label>
<label>
<input type="checkbox" name="interest" value="tips" /> Tips
</label>
<button type="submit">Sign up</button>
</form>
);
}get(name)returns the first value for a field, ornull.getAll(name)returns an array. Use it for checkboxes that share a name.- Values are strings, or
Fileobjects for file inputs. Convert numbers yourself.
Common errors
- Calling the function yourself. Write
action={saveProfile}, notaction={saveProfile()}. The second form runs the function during render and passes its result. - Missing
nameattributes. Fields without a name do not appear inFormData. - Expecting an event. The action receives
FormData, not a submit event. You cannot callevent.preventDefault()inside it. - Using a synchronous action and expecting a visible pending state. A function that returns immediately finishes before the user can see a pending period. Pending UI only shows when the action returns a promise that takes time.
Where this leads
You now have the base for the rest of the tutorial. Passing a function to action gives React a defined start and end for each submission. The next chapters use that to read the pending state with useFormStatus and to build a submit button that disables itself while the action runs.
Exercise
A developer writes <form action={saveProfile()}> and the profile is saved as soon as the page renders, before any submit. What is wrong, and what is the fix? Also name one reason formData.get('email') could return null even though the user typed an email.
Show answer
Parentheses call saveProfile during render, so the function runs immediately and its return value is passed to action. Pass the function itself: action={saveProfile}. formData.get('email') returns null when the input has no name="email" attribute, because React builds FormData from named fields only.