Skip to content

Commit 2e6dca1

Browse 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>
1 parent b87d2bd commit 2e6dca1

1 file changed

Lines changed: 61 additions & 2 deletions

File tree

‎src/content/reference/react-dom/hooks/useFormStatus.md‎

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ In the above example, `Submit` uses this information to disable `<button>` press
5454

5555
A `status` object with the following properties:
5656

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`.
5858

5959
* `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`.
6060

@@ -72,7 +72,7 @@ A `status` object with the following properties:
7272

7373
## Usage {/*usage*/}
7474

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*/}
7676
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.
7777

7878
Here, we use the `pending` property to indicate the form is submitting.
@@ -112,6 +112,65 @@ export async function submitForm(query) {
112112
```
113113
</Sandpack>
114114

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.
120+
121+
<Sandpack>
122+
123+
```js src/App.js
124+
import { startTransition } from 'react';
125+
import { useFormStatus } from 'react-dom';
126+
import { submitForm } from './submitForm.js';
127+
128+
function SubmitButton() {
129+
const { pending } = useFormStatus();
130+
return (
131+
<button type='submit' disabled={pending}>
132+
{pending ? 'Submitting...' : 'Submit'}
133+
</button>
134+
);
135+
}
136+
137+
function Form({ submitAction }) {
138+
function handleSubmit(e) {
139+
e.preventDefault();
140+
const formData = new FormData(e.currentTarget);
141+
startTransition(async () => {
142+
await submitAction(formData);
143+
});
144+
}
145+
146+
return (
147+
<form onSubmit={handleSubmit}>
148+
<input name='query' />
149+
<SubmitButton />
150+
</form>
151+
);
152+
}
153+
154+
export default function App() {
155+
async function submitAction(formData) {
156+
await submitForm(formData);
157+
}
158+
159+
return <Form submitAction={submitAction} />;
160+
}
161+
```
162+
163+
```js src/submitForm.js hidden
164+
export async function submitForm(formData) {
165+
await new Promise((resolve) => setTimeout(resolve, 1000));
166+
}
167+
```
168+
169+
</Sandpack>
170+
171+
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+
115174
<Pitfall>
116175

117176
##### `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

Comments
 (0)