Skip to content

Commit d8ff8dd

Browse files
committed
Polish useFormStatus onSubmit documentation
1 parent 0946026 commit d8ff8dd

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ export async function submitForm(query) {
112112
```
113113
</Sandpack>
114114

115-
### Display a pending state with a custom form action {/*display-a-pending-state-with-a-custom-form-action*/}
115+
---
116+
117+
### Display a pending state with an `onSubmit` handler {/*display-a-pending-state-with-an-onsubmit-handler*/}
116118

117-
If you need to handle form submission in an `onSubmit` event handler instead of passing a function to the `<form action>` prop, call `event.preventDefault()` and start a Transition in the same event handler. A component inside the form can then use `useFormStatus` to read the pending state while the action is running.
119+
If you need to handle form submission in an `onSubmit` event handler instead of passing a function to the `<form action>` prop, call `event.preventDefault()` and start a Transition in the same event handler. A component inside the form can then use `useFormStatus` to read the pending state while the Action is running.
118120

119121
<Sandpack>
120122

@@ -126,24 +128,24 @@ import { submitForm } from './submitForm.js';
126128
function SubmitButton() {
127129
const { pending } = useFormStatus();
128130
return (
129-
<button type="submit" disabled={pending}>
131+
<button type='submit' disabled={pending}>
130132
{pending ? 'Submitting...' : 'Submit'}
131133
</button>
132134
);
133135
}
134136

135137
function Form({ submitAction }) {
136-
function handleSubmit(event) {
137-
event.preventDefault();
138-
const formData = new FormData(event.currentTarget);
138+
function handleSubmit(e) {
139+
e.preventDefault();
140+
const formData = new FormData(e.currentTarget);
139141
startTransition(async () => {
140142
await submitAction(formData);
141143
});
142144
}
143145

144146
return (
145147
<form onSubmit={handleSubmit}>
146-
<input name="query" />
148+
<input name='query' />
147149
<SubmitButton />
148150
</form>
149151
);

0 commit comments

Comments
 (0)