-
Notifications
You must be signed in to change notification settings - Fork 45.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[React 19] allow opting out of automatic form reset #29034
Comments
I think you should return current values from action in such case and update the default value 😃 |
@adobe export issue to Jira project PWA |
I think you should return current values from action in such case and update the default value. and return required! |
This is very necessary in the step-by-step form, such as verifying the email in the auth form first |
Be careful to handle if the action throws an error, your "returning the new default" at the end of the function will be ineffective. |
The automatic form reset in React 19 actually caught me off guard, where in my case, I was trying to validate the form inputs on the server, then return & display the input errors on the client, but React will reset all my uncontrolled inputs. For context, I wrote a library just for doing server-side validation https://github.com/chungweileong94/server-act?tab=readme-ov-file#useformstate-support. I know that you can pass the original input ( It's easy to reset a form, but hard to restore a form. |
Now that I have played with React 19 form reset for a while, I think this behavior kind of forces us to write a more progressive enhancement code. This means that if you manually return the form data from the server and restore the form values, the user input will persist even without JavaScript enabled. Mixed feelings, pros and cons. |
what about using onSubmit as well the action to prevent default? |
If you want to opt-out of automatic form reset, you should continue using +function handleSubmit(event) {
+ event.preventDefault();
+ const formData = new FormData(event.target);
+ startTransition(() => action(formData));
+}
...
-<form action={action}>
+<form onSubmit={handleSubmit}> -- That way you still opt-into transitions but keep the old non-resetting behavior. And if you're a component library with your own action-based API that wants to maintain form-resetting behavior, you can use function onSubmit(event) {
// Disable default form submission behavior
event.preventDefault();
const form = event.target;
startTransition(async () => {
// Request the form to reset once the action
// has completed
ReactDOM.requestFormReset(form);
// Call the user-provided action prop
await action(new FormData(form));
})
} --https://codesandbox.io/p/sandbox/react-opt-out-of-automatic-form-resetting-45rywk We haven't documented that yet in https://react.dev/reference/react-dom/components/form. It would help us a lot if somebody would file a PR with form-resetting docs. |
Summary
repo: https://github.com/stefanprobst/issue-react-19-form-reset
react 19@beta currently will automatically reset a form with uncontrolled components after submission. it would be really cool if there was a way to opt out of this behavior, without having to fall back to using controlled components - especially since component libraries (e.g.
react-aria
) have invested quite a bit of time to work well as uncontrolled form elements.the main usecase i am thinking of are forms which allow saving progress, or saving a draft, before final submit. currently, every "save progress" would reset the form.
The text was updated successfully, but these errors were encountered: