Skip to content

Conversation

@rdanciu11
Copy link

@rdanciu11 rdanciu11 commented Dec 13, 2025

Forms were incorrectly being reset even when form actions threw errors. This was because requestFormReset was called before the action executed.

Now requestFormReset is only called if the action completes without throwing a synchronous error. For async actions, the form reset is scheduled immediately after the action is called (before it resolves), which is the correct behavior.

Added test to verify forms remain dirty when actions fail.

Fixes #35295

Summary

Fixes #35295

Forms were being reset even when form actions failed/threw errors. This was incorrect behavior according to the React documentation which states:

After the action function succeeds, all uncontrolled field elements in the form are reset.

Root Cause

In startHostTransition (line ~3270 in ReactFiberHooks.js), requestFormReset(formFiber) was being called before action(formData) executed, causing forms to reset regardless of whether the action succeeded or failed.

Solution

Wrapped the action call in try/catch so that requestFormReset is only called if the action doesn't throw a synchronous error. For async actions, the form reset is scheduled immediately after the action is called (before the promise resolves), which matches the existing behavior for successful async actions.

How did you test this change?

  1. Reproduced the bug using the CodeSandbox from issue Bug: Forms are reset when action fails #35295

    • Confirmed forms were incorrectly resetting when actions threw errors
  2. Verified the fix

    • All 44 existing tests in ReactDOMForm-test.js pass
    • Added new test case: "forms should not reset when action fails"
    • New test fails on main branch, passes with this fix
    • Manually tested the CodeSandbox example - forms now correctly preserve data when actions fail
  3. Ran test commands:

   yarn test ReactDOMForm-test
   # Result: 45/45 tests passing
  1. Confirmed no regressions:
    • Existing tests for successful actions still pass
    • Forms still reset correctly after successful actions
    • Error handling in actions works as expected

Forms were incorrectly being reset even when form actions threw errors.
This was because requestFormReset was called before the action executed.

Now requestFormReset is only called if the action completes without
throwing a synchronous error. For async actions, the form reset is
scheduled immediately after the action is called (before it resolves),
which is the correct behavior.

Added test to verify forms remain dirty when actions fail.

Fixes facebook#35295
@meta-cla
Copy link

meta-cla bot commented Dec 13, 2025

Hi @rdanciu11!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@meta-cla
Copy link

meta-cla bot commented Dec 13, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla bot added the CLA Signed label Dec 13, 2025
@meta-cla
Copy link

meta-cla bot commented Dec 13, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Forms are reset when action fails

1 participant