From 62f311ac502a90086d0891c1d2a6f0341d7b8725 Mon Sep 17 00:00:00 2001 From: Tom Sherman Date: Wed, 30 Apr 2025 15:04:23 +0100 Subject: [PATCH 1/2] Add example around disabling default form reset behaviour --- .../reference/react-dom/components/form.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/content/reference/react-dom/components/form.md b/src/content/reference/react-dom/components/form.md index 115e6a4cd20..b7b8a8c205d 100644 --- a/src/content/reference/react-dom/components/form.md +++ b/src/content/reference/react-dom/components/form.md @@ -71,6 +71,37 @@ 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. + + + +```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. From b624c30046cf5ad12318e276e3106212a58198c4 Mon Sep 17 00:00:00 2001 From: Tom Sherman Date: Wed, 30 Apr 2025 15:12:31 +0100 Subject: [PATCH 2/2] Add what to watch out for in the example --- src/content/reference/react-dom/components/form.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/content/reference/react-dom/components/form.md b/src/content/reference/react-dom/components/form.md index b7b8a8c205d..8befa2ada4a 100644 --- a/src/content/reference/react-dom/components/form.md +++ b/src/content/reference/react-dom/components/form.md @@ -73,6 +73,8 @@ 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