diff --git a/src/content/reference/react-dom/components/form.md b/src/content/reference/react-dom/components/form.md index 115e6a4cd20..8befa2ada4a 100644 --- a/src/content/reference/react-dom/components/form.md +++ b/src/content/reference/react-dom/components/form.md @@ -71,6 +71,39 @@ export default function Search() { +You can override the default reset behaviour of the form by additionally passing an `onSubmit` prop to the `
` component calls `preventDefault` on the event and calling the action yourself. Passing `onSubmit` alongside `action` ensures the form can be progressively enhanced if this component is rendered to HTML on the server. + +Notice how in the below example now the form is not reset and your input is preserved after the form is submitted. + + + +```js +import { startTransition } from "react"; +export default function Search() { + function search(formData) { + const query = formData.get("query"); + alert(`You searched for '${query}'`); + } + return ( + { + event.preventDefault(); + const formData = new FormData(event.currentTarget); + startTransition(() => { + search(formData); + }); + }} + > + + + + ); +} +``` + +
+ ### Handle form submission with a Server Function {/*handle-form-submission-with-a-server-function*/} Render a `
` with an input and submit button. Pass a Server Function (a function marked with [`'use server'`](/reference/rsc/use-server)) to the `action` prop of form to run the function when the form is submitted.