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
{{ message }}
Repository navigation
Commit 2e6dca1
Browse filesBrowse the repository at this point in the historyBrowse files
docs: explain useFormStatus with custom form actions (#8683)
* docs: explain useFormStatus with custom form actions
Closes#8682.
Document when `useFormStatus().pending` is set for a `preventDefault()` submit handler that starts a Transition in the same event, and add a runnable example for a custom form action. The example also notes that this pattern does not reset the form automatically.
Validation: `yarn check-all` and `git diff --check` pass locally. The initial draft was prepared with AI assistance and reviewed against the React DOM tests and issue details.
* Polish useFormStatus onSubmit documentation
* Clarify pending status and link form reset guidance
* Shorten pending definition and link onSubmit guidance
* Link form submission and Transition references
* Make form pending-state headings parallel
---------
Co-authored-by: Aurora Scharff <aurora.sofie@gmail.com>
Copy file name to clipboardExpand all lines: src/content/reference/react-dom/hooks/useFormStatus.md
+61-2Lines changed: 61 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,7 @@ In the above example, `Submit` uses this information to disable `<button>` press
54
54
55
55
A `status` object with the following properties:
56
56
57
-
*`pending`: A boolean. If `true`, this means the parent `<form>` is pending submission. Otherwise, `false`.
57
+
*`pending`: A boolean. `true` while the parent `<form>` is submitting an Action or [running a Transition from `onSubmit`](#display-a-pending-state-with-an-onsubmit-handler). Otherwise, `false`.
58
58
59
59
*`data`: An object implementing the [`FormData interface`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) that contains the data the parent `<form>` is submitting. If there is no active submission or no parent `<form>`, it will be `null`.
60
60
@@ -72,7 +72,7 @@ A `status` object with the following properties:
72
72
73
73
## Usage {/*usage*/}
74
74
75
-
### Display a pending state during form submission {/*display-a-pending-state-during-form-submission*/}
75
+
### Display a pending state with an `action` prop {/*display-a-pending-state-during-form-submission*/}
76
76
To display a pending state while a form is submitting, you can call the `useFormStatus` Hook in a component rendered in a `<form>` and read the `pending` property returned.
77
77
78
78
Here, we use the `pending` property to indicate the form is submitting.
@@ -112,6 +112,65 @@ export async function submitForm(query) {
112
112
```
113
113
</Sandpack>
114
114
115
+
---
116
+
117
+
### Display a pending state with an `onSubmit` handler {/*display-a-pending-state-with-an-onsubmit-handler*/}
118
+
119
+
If you need to handle form submission in an `onSubmit` event handler instead of passing a function to the [`<form action>` prop](/reference/react-dom/components/form#handle-form-submission-with-an-action-prop), call [`event.preventDefault()`](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault) and [start a Transition](/reference/react/startTransition) in the same event handler. A component inside the form can then use `useFormStatus` to read the pending state while the Action is running.
Unlike a function passed to the `<form action>` prop, handling submission this way [does not automatically reset the form](/reference/react-dom/components/form#other-ways-to-control-form-resets).
172
+
173
+
115
174
<Pitfall>
116
175
117
176
##### `useFormStatus` will not return status information for a `<form>` rendered in the same component. {/*useformstatus-will-not-return-status-information-for-a-form-rendered-in-the-same-component*/}
0 commit comments