Repository navigation
[react-dom] Preserve controlled <select> value on automatic form reset - #38447
SandeepDirishala wants to merge 2 commits into
Conversation
After a form action completes, React performs a native form.reset(). That relies on DOM defaults already tracking controlled props: <input> and <textarea> mirror their value into defaultValue, but <select> keeps its selection on the options and passes setDefaultSelected: false on the controlled path, so option.defaultSelected never tracks the React value. After the reset the select shows its DOM default while the fiber still holds the previous memoizedProps.value, so the next render diffs clean and the DOM is left out of sync for good. Reapply controlled state from the current props once the native reset has run. This is a no-op for <input> and <textarea>, whose defaults already match, and for uncontrolled elements, which updateInput leaves alone when props.value is null. Fixes react#30580
|
Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Summary
Fixes #30580.
After a form action completes, React calls
requestFormReset, which performs a nativeform.reset(). React's design assumes DOM defaults already track controlled props — as the comment inReactFiberCommitWorkputs it, the reset is deferred until after all other mutations "so thatdefaultValuewill already be updated."<input>and<textarea>uphold that by mirroring their controlledvalueintonode.defaultValue.<select>can't: its selection lives on the options, and the controlled path inupdateSelect/initSelectpassessetDefaultSelected: false, sooption.defaultSelectednever tracks the React value.So after the reset the select displays its DOM default (the first non-disabled option) while the fiber still holds the previous
memoizedProps.value. The next render diffs clean, nothing repairs the DOM, and the controlled component stays desynced from its props permanently. There is no fallback either: no plugin registersresetwith the controlled-state restore queue, so the existingrestoreControlledStateis unreachable on this path.This reapplies controlled state from the current props once the native reset has run. It is a no-op for
<input>and<textarea>, whose defaults already match their props, and for uncontrolled elements, sinceupdateInputonly touchesnode.valuewhenprops.value != null— uncontrolled fields still reset normally.How did you test this?
Added
preserves a controlled select on automatic form resettoReactDOMForm-test.js. It fails onmain:and passes with the fix.
Also ran
ReactDOMForm-test,ReactDOMSelect-test,ReactDOMInput-testandReactDOMTextarea-testtogether (284 passed, 0 failed), plusyarn lintand a prettier check on the changed files.Worth noting that
ReactDOMForm-test.jspreviously had six automatic-reset tests, all using<input>;<select>did not appear in the file at all.