Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions src/content/reference/react-dom/hooks/useFormStatus.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ In the above example, `Submit` uses this information to disable `<button>` press

A `status` object with the following properties:

* `pending`: A boolean. If `true`, this means the parent `<form>` is pending submission. Otherwise, `false`.
* `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`.

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

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

## Usage {/*usage*/}

### Display a pending state during form submission {/*display-a-pending-state-during-form-submission*/}
### Display a pending state with an `action` prop {/*display-a-pending-state-during-form-submission*/}
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.

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

---

### Display a pending state with an `onSubmit` handler {/*display-a-pending-state-with-an-onsubmit-handler*/}

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.

<Sandpack>

```js src/App.js
import { startTransition } from 'react';
import { useFormStatus } from 'react-dom';
import { submitForm } from './submitForm.js';

function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type='submit' disabled={pending}>
{pending ? 'Submitting...' : 'Submit'}
</button>
);
}

function Form({ submitAction }) {
function handleSubmit(e) {
e.preventDefault();
const formData = new FormData(e.currentTarget);
startTransition(async () => {
await submitAction(formData);
});
}

return (
<form onSubmit={handleSubmit}>
<input name='query' />
<SubmitButton />
</form>
);
}

export default function App() {
async function submitAction(formData) {
await submitForm(formData);
}

return <Form submitAction={submitAction} />;
}
```

```js src/submitForm.js hidden
export async function submitForm(formData) {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
```

</Sandpack>

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).


<Pitfall>

##### `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*/}
Expand Down
Loading