Skip to content

Commit b6e9b3c

Browse files
committed
[form] Document onSubmit event handler alongside action
1 parent fb3854a commit b6e9b3c

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

  • src/content/reference/react-dom/components

src/content/reference/react-dom/components/form.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,44 @@ To create interactive controls for submitting information, render the [built-in
4848

4949
## Usage {/*usage*/}
5050

51-
### Handle form submission on the client {/*handle-form-submission-on-the-client*/}
51+
### Handle form submission with an event handler {/*handle-form-submission-with-an-event-handler*/}
5252

53-
Pass a function to the `action` prop of form to run the function when the form is submitted. [`formData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) will be passed to the function as an argument so you can access the data submitted by the form. This differs from the conventional [HTML action](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#action), which only accepts URLs. After the `action` function succeeds, all uncontrolled field elements in the form are reset.
53+
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, so call [`e.preventDefault()`](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault) to override that behavior. Read the submitted data with [`new FormData(e.target)`](https://developer.mozilla.org/en-US/docs/Web/API/FormData).
54+
55+
<Sandpack>
56+
57+
```js src/App.js
58+
export default function Search() {
59+
function handleSubmit(e) {
60+
// Prevent the browser from reloading the page
61+
e.preventDefault();
62+
63+
// Read the form data
64+
const formData = new FormData(e.target);
65+
const query = formData.get("query");
66+
alert(`You searched for '${query}'`);
67+
}
68+
69+
return (
70+
<form onSubmit={handleSubmit}>
71+
<input name="query" />
72+
<button type="submit">Search</button>
73+
</form>
74+
);
75+
}
76+
```
77+
78+
</Sandpack>
79+
80+
<Note>
81+
82+
Reading form data with `onSubmit` works in every version of React and gives you direct access to the [submit event](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event). The `action` prop, described next, builds on this with extra features like automatic form resets, pending states with [`useFormStatus`](/reference/react-dom/hooks/useFormStatus), and [Server Functions](/reference/rsc/server-functions).
83+
84+
</Note>
85+
86+
### Handle form submission with an action {/*handle-form-submission-with-an-action*/}
87+
88+
Pass a function to the `action` prop of form to run the function when the form is submitted. [`formData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) will be passed to the function as an argument so you can access the data submitted by the form. This differs from the conventional [HTML action](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#action), which only accepts URLs. Unlike `onSubmit`, an `action` runs in a [Transition](/reference/react/useTransition) and calling `e.preventDefault()` isn't needed. After the `action` function succeeds, all uncontrolled field elements in the form are reset.
5489

5590
<Sandpack>
5691

0 commit comments

Comments
 (0)