More React answers

by Danny
Published 2 min read

React 19 clears uncontrolled fields after a form action finishes, even when it returns a validation error. Here is why, and how to keep what the user typed.

The short answer

#

When you pass a function to a form's action prop, React 19 resets the form's uncontrolled fields once the action finishes. It does this even if your action returned an error message, so the user loses what they typed. Return the submitted values from the action and feed them back in as defaultValue, or handle the submission yourself with onSubmit.


_10
<input name="email" defaultValue={state.values.email} />

Why it happens

#

Form actions are modelled on plain HTML forms. When a normal form submits, the browser loads a new page and the fields start empty. React copies that behaviour: after the action's transition completes, it resets the form, which puts every uncontrolled input back to its defaultValue.

React cannot tell whether a result counts as success in your app. An action that returns an object with an error message has still finished normally, so the reset still happens. Controlled inputs, where you pass value and keep the text in state, are not affected, because React renders them from your state again.

How to fix it

#

Return the submitted values and use them as defaultValue

This keeps the fields uncontrolled and works with Server Actions. The action returns what the user entered, and the reset restores each field to that value instead of an empty string.


_24
// app/subscribe/actions.ts
_24
'use server';
_24
_24
export type FormState = {
_24
error?: string;
_24
values: { name: string; email: string };
_24
};
_24
_24
export async function subscribe(
_24
prevState: FormState,
_24
formData: FormData
_24
): Promise<FormState> {
_24
const values = {
_24
name: String(formData.get('name') ?? ''),
_24
email: String(formData.get('email') ?? ''),
_24
};
_24
_24
if (!values.email.includes('@')) {
_24
return { error: 'Please enter a valid email address.', values };
_24
}
_24
_24
await saveSubscriber(values);
_24
return { values: { name: '', email: '' } };
_24
}


_22
// app/subscribe/subscribe-form.tsx
_22
'use client';
_22
_22
import { useActionState } from 'react';
_22
import { subscribe, type FormState } from './actions';
_22
_22
const initialState: FormState = { values: { name: '', email: '' } };
_22
_22
export function SubscribeForm() {
_22
const [state, formAction, isPending] = useActionState(subscribe, initialState);
_22
_22
return (
_22
<form action={formAction}>
_22
<input name="name" defaultValue={state.values.name} />
_22
<input name="email" type="email" defaultValue={state.values.email} />
_22
{state.error && <p role="alert">{state.error}</p>}
_22
<button type="submit" disabled={isPending}>
_22
Subscribe
_22
</button>
_22
</form>
_22
);
_22
}

On success the action returns empty values, so the form clears as expected.

Handle the submission with onSubmit

The automatic reset only applies when a function is passed to action. If you call preventDefault in onSubmit and run the work yourself, React leaves the fields alone, and you decide when to clear them:


_30
'use client';
_30
_30
import { useTransition, type FormEvent } from 'react';
_30
import { saveSubscriber } from './actions';
_30
_30
export function SubscribeForm() {
_30
const [isPending, startTransition] = useTransition();
_30
_30
function handleSubmit(event: FormEvent<HTMLFormElement>) {
_30
event.preventDefault();
_30
const form = event.currentTarget;
_30
const formData = new FormData(form);
_30
_30
startTransition(async () => {
_30
const result = await saveSubscriber(formData);
_30
if (result.ok) {
_30
form.reset();
_30
}
_30
});
_30
}
_30
_30
return (
_30
<form onSubmit={handleSubmit}>
_30
<input name="email" type="email" />
_30
<button type="submit" disabled={isPending}>
_30
Subscribe
_30
</button>
_30
</form>
_30
);
_30
}

You lose the progressive enhancement that form actions give you, because this form needs JavaScript to submit.

Use controlled inputs for complex forms

If you already keep fields in state for live validation, pass value and onChange. The reset does not change controlled values.

Common pitfalls

#
  • Passing value without onChange. React makes the input read-only and warns in the console. Use defaultValue for uncontrolled fields.
  • Forgetting selects and checkboxes. Use defaultValue on a select and defaultChecked on a checkbox in the same way.
  • Sending sensitive fields back to the browser. Do not return passwords or card details in the state; let those fields clear.
  • Expecting a new defaultValue to change a field the user is editing. It only takes effect on first render and when the form resets.
  • Trying to refill a file input. Browsers do not allow it, so ask the user to choose the file again.
#

Was this resource helpful?