You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -42,8 +42,8 @@ To create interactive controls for submitting information, render the [built-in
42
42
* If you pass a function to `action`, React runs it in a [Transition](/reference/react/useTransition) following [the Action prop pattern](/reference/react/useTransition#exposing-action-props-from-components).
43
43
* The function may be async. React calls it with a single argument containing the [form data](https://developer.mozilla.org/en-US/docs/Web/API/FormData) of the submitted form.
44
44
* A `formAction` prop on a `<button>`, `<input type="submit">`, or `<input type="image">` overrides this `action`.
45
-
*[`method`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#method): A string. Specifies the [HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) (`get` or `post`). Defaults to `get`. Ignored when `action` is a function.
46
-
*`onSubmit`: An [`Event` handler](/reference/react-dom/components/common#event-handler) function. Fires when the form is submitted. If you also pass a function to `action`, both run unless you call `e.preventDefault()`. See [Handling form submission with an event handler](#handle-form-submission-with-an-event-handler).
45
+
*[`method`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#method): A string that specifies the HTTP methodto use when `action` is a URL. Defaults to `get`.
46
+
*`onSubmit`: An [`Event` handler](/reference/react-dom/components/common#event-handler) function. Fires when the form is submitted. See [Handling form submission with an event handler](#handle-form-submission-with-an-event-handler).
47
47
48
48
#### Caveats {/*caveats*/}
49
49
@@ -58,6 +58,8 @@ To create interactive controls for submitting information, render the [built-in
58
58
59
59
Pass a function to the `onSubmit` event handler to run code when the form is submitted. By default, the browser sends the form data to the current URL and refreshes the page. Calling [`e.preventDefault()`](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault) in the event handler overrides this behavior.
60
60
61
+
If you also pass a function to `action`, React runs it after `onSubmit` unless `onSubmit` calls `e.preventDefault()`.
62
+
61
63
<Sandpack>
62
64
63
65
```js src/App.js
@@ -92,7 +94,7 @@ Reading form data with `onSubmit` works in every version of React and gives you
92
94
93
95
### Handling form submission with an action prop {/*handle-form-submission-with-an-action-prop*/}
94
96
95
-
Pass a function to the `action` prop to run it when the form is submitted. React calls the function with a [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) object containing the values of every input with a `name` attribute. Your inputs can be [uncontrolled](/reference/react-dom/components/input#reading-the-input-values-when-submitting-a-form)-you don't need `value`/`onChange` pairs, an `onSubmit` handler, or `e.preventDefault()`.
97
+
Pass a function to the `action` prop to run it when the form is submitted. React calls the function with a [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) object containing the values of every input with a `name` attribute. Your inputs can be [uncontrolled](/reference/react-dom/components/input#reading-the-input-values-when-submitting-a-form). You don't need `value`/`onChange` pairs, an `onSubmit` handler, or `e.preventDefault()`.
96
98
97
99
When you pass a function to `action`, React:
98
100
@@ -126,11 +128,11 @@ export default function Search() {
126
128
127
129
### Handling form submission with a Server Function {/*handle-form-submission-with-a-server-function*/}
128
130
129
-
Render a `<form>` 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.
131
+
Render a `<form>` with an input and submit button. Pass a Server Function (a function marked with [`'use server'`](/reference/rsc/use-server)) to the form's `action` prop to run the function when the form is submitted.
130
132
131
-
Passing a Server Function to `<formaction>`allows users to submit forms without JavaScript enabled or before the code has loaded. This is beneficial to users who have a slow connection, device, or have JavaScript disabled and is similar to the way forms work when a URL is passed to the `action` prop.
133
+
Passing a Server Function to a form's `action` prop allows users to submit the form before JavaScript loads or when JavaScript is disabled. This matches how forms behave when you pass a URL to `action`.
132
134
133
-
You can use hidden form fields to provide data to the `<form>`'s action. The Server Function will be called with the hidden form field data as an instance of [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData).
135
+
You can use hidden form fields to pass data to the Server Function. React includes the hidden field values in the [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) passed to the function.
134
136
135
137
```jsx
136
138
import { updateCart } from'./lib.js';
@@ -150,7 +152,7 @@ function AddToCart({productId}) {
150
152
}
151
153
```
152
154
153
-
In lieu of using hidden form fields to provide data to the `<form>`'s action, you can call the <CodeStepstep={1}>`bind`</CodeStep> method to supply it with extra arguments. This will bind a new argument (<CodeStepstep={2}>`productId`</CodeStep>) to the function in addition to the <CodeStepstep={3}>`formData`</CodeStep> that is passed as an argument to the function.
155
+
Instead of using a hidden form field, call the <CodeStepstep={1}>`bind`</CodeStep> method to pass an extra argument to the Server Function. This binds <CodeStepstep={2}>`productId`</CodeStep> as an argument before the <CodeStepstep={3}>`formData`</CodeStep> that React passes to the function.
@@ -290,7 +292,7 @@ To learn more about the `useOptimistic` Hook, see the [reference documentation](
290
292
291
293
### Handling form submission errors {/*handling-form-submission-errors*/}
292
294
293
-
In some cases the function called by a `<form>`'s `action` prop throws an error. You can handle these errors by wrapping `<form>` in an Error Boundary. If the Action throws, the Error Boundary fallback will be displayed.
295
+
To handle errors thrown by a function passed to a `<form>`'s `action` prop, wrap the form in an Error Boundary. React displays the boundary's fallback when the function throws.
294
296
295
297
<Sandpack>
296
298
@@ -338,69 +340,58 @@ Displaying a form submission error message before the JavaScript bundle loads fo
338
340
1. the function passed to the `<form>`'s `action` prop be a [Server Function](/reference/rsc/server-functions)
339
341
1. the `useActionState` Hook be used to display the error message
340
342
341
-
`useActionState` takes two parameters: a [Server Function](/reference/rsc/server-functions) and an initial state. `useActionState` returns two values, a state variable and an Action. The Action returned by `useActionState` should be passed to the `action` prop of the form. The state variable returned by `useActionState` can be used to display an error message. The value returned by the Server Function passed to `useActionState` will be used to update the state variable.
343
+
Define the Server Function in a separate file with the [`'use server'`](/reference/rsc/use-server) directive. It receives the previous state followed by the submitted `FormData`:
In a Client Component, pass the Server Function to `useActionState`. Pass the returned Action to the form's `action` prop and render the returned state:
378
363
379
-
exportasyncfunctionsignUpNewUser(newEmail) {
380
-
if (emails.includes(newEmail)) {
381
-
thrownewError('This email address has already been added');
To learn more about updating state from a form Action, see the [`useActionState`](/reference/react/useActionState) docs.
384
+
If the form is submitted before JavaScript loads, React includes the Server Function's returned error message in the server-rendered response.
390
385
391
386
---
392
387
393
388
### Preserving form values after submission {/*preserve-form-values-after-submission*/}
394
389
395
-
By default, the browser clears a form's input state after submission. Forms with a URL `action` follow this behavior, and React mirrors it when `action` is a function so the form behaves consistently before and after JavaScript loads.
396
-
397
-
When you pass a function to `action` or `formAction`, React resets the form's [uncontrolled fields](/reference/react-dom/components/input#reading-the-input-values-when-submitting-a-form) after the Action succeeds. This reset only affects uncontrolled fields-[inputs controlled with state](/reference/react-dom/components/input#controlling-an-input-with-a-state-variable) are not cleared.
398
-
399
-
<RecipestitleText="Examples of preserving form values"titleId="examples-preserve-form-values">
390
+
Submitting a form with a URL `action` clears its input state. React mirrors this behavior when `action` is a function by resetting the form's [uncontrolled fields](/reference/react-dom/components/input#reading-the-input-values-when-submitting-a-form) after the Action succeeds. When a Server Function progressively enhances a form, this keeps its behavior consistent before and after JavaScript loads. [Inputs controlled with state](/reference/react-dom/components/input#controlling-an-input-with-a-state-variable) are not cleared.
400
391
401
392
#### Restore fields with `useActionState` {/*with-useactionstate*/}
402
393
403
-
Pass the action returned by [`useActionState`](/reference/react/useActionState) to the `action` prop. Return the values you want to keep from your Action, and pass them to each field's `defaultValue`. React restores those values instead of clearing them.
394
+
Pass the Action returned by [`useActionState`](/reference/react/useActionState) to the `action` prop. Return the values you want to keep from your Action, and pass them to each field's `defaultValue`. The automatic form reset restores those default values instead of clearing the fields.
404
395
405
396
<Sandpack>
406
397
@@ -435,95 +426,27 @@ export async function submitForm(previousState, formData) {
435
426
436
427
</Sandpack>
437
428
438
-
<Solution />
439
-
440
-
#### Keep every field with `onSubmit` {/*with-onsubmit-and-usetransition*/}
441
-
442
-
Call `e.preventDefault()` in an `onSubmit` handler and run the Action yourself with [`startTransition`](/reference/react/useTransition). React doesn't reset the form because the `action` prop never runs. Keep passing `action` so the form still submits before JavaScript loads.
You can also reset only some fields, or restore values from the server on validation failure.
486
-
487
429
<DeepDive>
488
430
489
-
#### Resetting only some fields, or resetting on the server {/*resetting-only-some-fields*/}
431
+
#### Choosing how to manage form values {/*choosing-how-to-manage-form-values*/}
490
432
491
-
The `onSubmit` approach above keeps every uncontrolled field. For finer control, you can:
433
+
Choose an approach based on what should happen after submission:
492
434
493
-
***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 [`requestFormReset`](/blog/2024/12/05/react-19#form-actions) from `react-dom` with the form element inside the Transition.
435
+
***Preserve selected values with `useActionState`.**The example above returns the submitted title after every submission. To preserve values only when validation fails, return the submitted `FormData` in the error state and use it to set each field's `defaultValue`. With a Server Function, React can include those values in the server response before JavaScript loads.
494
436
495
-
***Reset to server-provided values on validation failure.** The [`useActionState`](#with-useactionstate) example above preserves values after a successful submission. When an Action validates input on the server, you can 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:
496
-
497
-
```js
498
-
import { useActionState } from'react';
499
-
import { submitForm } from'./actions.js';
437
+
***Keep every value with `onSubmit`.** Call `e.preventDefault()`, then run the Action inside [`startTransition`](/reference/react/useTransition). Calling `preventDefault()` prevents the function passed to the form's `action` prop from running for that submission, so React does not automatically reset the form.
500
438
501
-
functionEditForm() {
502
-
// The Action returns { submitted: formData, error } on failure
***Reset fields at a specific point.** Call the form element's [`reset()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset) method to immediately reset uncontrolled fields to their default values. To schedule the same reset inside an Action or Transition, call [`requestFormReset`](/blog/2024/12/05/react-19#form-actions) from `react-dom`.
515
440
516
-
Return the original `FormData` object rather than a new one so React can restore the values even before JavaScript has loaded.
441
+
***Reset the fields and component state.** Change the [`key`](/learn/preserving-and-resetting-state#resetting-a-form-with-a-key) on the component that renders the form. React recreates the component and its DOM, so its fields and local state both start over.
A form can have more than one submit button, each running a different Action. Set the `formAction` prop on a `<button>` to override the `<form>`'s `action` when that button submits the form.
525
-
526
-
When a button without `formAction` submits the form, React calls the form's `action`. When a button with `formAction` submits the form, React calls that button's `formAction` instead. For example, the form below publishes an article by default, but its **Save draft** button stores the current content without publishing it:
449
+
A form can have more than one submit button, each running a different Action. A button without `formAction` runs the form's `action`; a button with `formAction` runs its own Action instead. For example, the form below publishes an article by default, but its **Save draft** button stores the current content without publishing it:
Copy file name to clipboardExpand all lines: src/content/reference/react/useActionState.md
+60-3Lines changed: 60 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1184,14 +1184,71 @@ hr {
1184
1184
1185
1185
In this example, when the user clicks the stepper arrows, the button submits the form and `useActionState` calls `updateCartAction` with the form data. The example uses `useOptimistic` to immediately show the new quantity while the server confirms the update.
1186
1186
1187
+
#### Displaying validation errors and preserving form values {/*displaying-validation-errors-and-preserving-form-values*/}
1188
+
1189
+
Return validation errors and submitted values from the Action to display an error without clearing the affected fields. Try submitting a name with fewer than three characters:
if (typeof name !=='string'||name.trim().length<3) {
1230
+
return {
1231
+
error:'Name must be at least three characters long',
1232
+
submitted: formData,
1233
+
};
1234
+
}
1235
+
return {
1236
+
error:null,
1237
+
submitted:null,
1238
+
};
1239
+
}
1240
+
```
1241
+
1242
+
</Sandpack>
1243
+
1244
+
When validation fails, `updateName` returns an error and the submitted `FormData`. The component uses the submitted name as the input's new `defaultValue`, so React's automatic form reset preserves it. When validation succeeds, `submitted` is `null`, so the automatic reset clears the field.
1245
+
1187
1246
<RSC>
1188
1247
1189
-
When used with a [Server Function](/reference/rsc/server-functions), `useActionState` allows the server's response to be shown before hydration (when React attaches to server-rendered HTML) completes. You can also use the optional `permalink` parameter for progressive enhancement (allowing the form to work before JavaScript loads) on pages with dynamic content. This is typically handled by your framework for you.
1248
+
When the `reducerAction` passed to `useActionState` is a [Server Function](/reference/rsc/server-functions), pass the returned `submitAction` to the form's `action` prop. React can then display the Server Function's returned value before hydration completes. Return the submitted `FormData` and use it to set each field's `defaultValue`to preserve those values before JavaScript loads. On pages with dynamic content, you can also use the optional `permalink` parameter for progressive enhancement. This is typically handled by your framework for you.
1190
1249
1191
1250
</RSC>
1192
1251
1193
-
See the [`<form>`](/reference/react-dom/components/form#handle-form-submission-with-a-server-function) docs for more information on using Actions with forms.
0 commit comments