Skip to content

Commit 4f39d4b

Browse files
committed
clean up preserving form values after submission
1 parent b341728 commit 4f39d4b

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

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

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ When `<form>` is rendered by a [Server Component](/reference/rsc/use-client), an
167167

168168
### Preserving form values after submission {/*preserve-form-values-after-submission*/}
169169

170-
The browser clears a form's input state on submit. A URL `action` follows this same behavior. React does the same when `action` is a function, so your form works consistently before and after JavaScript loads.
170+
By default, the browser clears a form's input state after submission. Forms with a URL `action` follow this behavior, and React mirrors it when `action` is a function-ensuring your form behaves consistently both before and after JavaScript loads.
171171

172-
When you pass a function to `action` or `formAction`, React resets the form's [uncontrolled fields](/reference/react-dom/components/input#reading-the-input-values-when-submitting-a-form) after the action succeeds. The reset only applies to uncontrolled fields, so [inputs you control with state](/reference/react-dom/components/input#controlling-an-input-with-a-state-variable) are never cleared.
172+
When you pass a function to `action` or `formAction`, React resets the form's [uncontrolled fields](/reference/react-dom/components/input#reading-the-input-values-when-submitting-a-form) after the action succeeds. This reset only affects uncontrolled fields[inputs controlled with state](/reference/react-dom/components/input#controlling-an-input-with-a-state-variable) are never cleared.
173173

174-
To keep the values of uncontrolled fields, add an `onSubmit` handler that calls `e.preventDefault()` and runs the action inside a [Transition](/reference/react/useTransition). Keep the `action` prop on the form so it still works before JavaScript loads.
174+
To preserve the values of uncontrolled fields after submission, add an `onSubmit` handler that calls `e.preventDefault()` and runs the action inside a [Transition](/reference/react/useTransition). Keep the `action` prop on the form so it continues to work before JavaScript loads.
175175

176176
<Sandpack>
177177

@@ -210,8 +210,6 @@ export async function submitForm(formData) {
210210

211211
</Sandpack>
212212

213-
Because you call the action manually from `onSubmit`, [`useFormStatus`](/reference/react-dom/hooks/useFormStatus) won't report its pending state. Read `isPending` from the same [`useTransition`](/reference/react/useTransition) call instead.
214-
215213
<DeepDive>
216214

217215
#### Resetting only some fields, or resetting on the server {/*resetting-only-some-fields*/}
@@ -228,11 +226,12 @@ import { submitForm } from "./actions.js";
228226

229227
function EditForm() {
230228
// The action returns { submitted: formData, error } on failure
231-
const [state, formAction] = useActionState(submitForm, {});
232-
const values = state.submitted ?? new FormData();
229+
const [state, formAction] = useActionState(submitForm, {
230+
error: '',
231+
});
233232
return (
234233
<form action={formAction}>
235-
<input name="title" defaultValue={values.get("title") ?? ""} />
234+
<input name="title" defaultValue={state.submitted?.get("title") ?? ""} />
236235
{state.error && <p>{state.error}</p>}
237236
<button type="submit">Save</button>
238237
</form>

0 commit comments

Comments
 (0)