From 5245923e754621ca6c0f0fb33fa74287f17441a6 Mon Sep 17 00:00:00 2001 From: Maxwell Cohen Date: Mon, 29 Jun 2026 09:40:46 -0500 Subject: [PATCH 1/2] Enhance form documentation for form default reset behavior --- .../reference/react-dom/components/form.md | 124 ++++++++++++++++-- 1 file changed, 111 insertions(+), 13 deletions(-) diff --git a/src/content/reference/react-dom/components/form.md b/src/content/reference/react-dom/components/form.md index 10e9c67940e..4d6daa8cac1 100644 --- a/src/content/reference/react-dom/components/form.md +++ b/src/content/reference/react-dom/components/form.md @@ -38,11 +38,12 @@ To create interactive controls for submitting information, render the [built-in `
` supports all [common element props.](/reference/react-dom/components/common#common-props) -[`action`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#action): a URL or function. When a URL is passed to `action` the form will behave like the HTML form component. When a function is passed to `action` the function will handle the form submission in a Transition following [the Action prop pattern](/reference/react/useTransition#exposing-action-props-from-components). The function passed to `action` may be async and will be called with a single argument containing the [form data](https://developer.mozilla.org/en-US/docs/Web/API/FormData) of the submitted form. The `action` prop can be overridden by a `formAction` attribute on a ` +
+ ); +} +``` + +```js src/api.js hidden +export async function submitForm(formData) { + await new Promise((res) => setTimeout(res, 1000)); +} +``` + + + +Because you call the action manually from `onSubmit`, [`useFormStatus`](/reference/react-dom/hooks/useFormStatus) won't report its pending state. Read `isPending` from the same [`useTransition`](/reference/react/useTransition) call instead. + + + +#### Resetting only some fields, or resetting on the server {/*resetting-only-some-fields*/} + +The `onSubmit` approach keeps every uncontrolled field. When you need finer control, two other patterns are available: + +* **Reset from your own action API.** If you build an action-based API and still want the form to reset after the action runs, call the `requestFormReset` API from `react-dom` with the form element inside the Transition. + +* **Reset to server-provided values.** When an action validates input on the server, return the submitted `FormData` and pass it to each field's `defaultValue`. React restores those values instead of clearing them, and the form keeps working before JavaScript loads: + +```js +import { useActionState } from "react"; +import { submitForm } from "./actions.js"; + +function EditForm() { + // The action returns { submitted: formData, error } on failure + const [state, formAction] = useActionState(submitForm, {}); + const values = state.submitted ?? new FormData(); + return ( +
+ + {state.error &&

{state.error}

} + +
+ ); +} +``` + +Return the original `FormData` object rather than a new one so React can restore the values even before JavaScript has loaded. + +
+ ### Handle form submission with a Server Function {/*handle-form-submission-with-a-server-function*/} Render a `
` with an input and submit button. Pass a Server Function (a function marked with [`'use server'`](/reference/rsc/use-server)) to the `action` prop of form to run the function when the form is submitted. @@ -372,28 +453,45 @@ Learn more about updating state from a form action with the [`useActionState`](/ ### Handling multiple submission types {/*handling-multiple-submission-types*/} -Forms can be designed to handle multiple submission actions based on the button pressed by the user. Each button inside a form can be associated with a distinct action or behavior by setting the `formAction` prop. +A form can have more than one submit button, each running a different action. Set the `formAction` prop on a `