Fix: Forms should not reset when action fails #35354
Open
+56
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Root Cause
In
startHostTransition(line ~3270 inReactFiberHooks.js),requestFormReset(formFiber)was being called beforeaction(formData)executed, causing forms to reset regardless of whether the action succeeded or failed.Solution
Wrapped the action call in try/catch so that
requestFormResetis 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?
Reproduced the bug using the CodeSandbox from issue Bug: Forms are reset when action fails #35295
Verified the fix
ReactDOMForm-test.jspassRan test commands: