ISSUE DESCRIPTION issue: values data is reset after first render, when shouldUnregister…
- User
ISSUE DESCRIPTION
issue:
valuesdata is reset after first render, whenshouldUnregisteris providedVersion Number
7.71.2
Codesandbox/Expo snack
https://codesandbox.io/p/sandbox/blazing-dust-3225h9?file=%2Fsrc%2FApp.js
Steps to reproduce
- Simply submit a form
- See error
Expected behaviour
It is possible to successfully submit a form, when data is provided via
values, andshouldUnregister: trueoption is setWhat browsers are you seeing the problem on?
Chrome
Code of Conduct
- I agree to follow this project's Code of Conduct
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
- Assistant
I’ll inspect the repository structure and current implementation around
valuesandshouldUnregister, then identify relevant tests and reproduce the state reset before making a targeted fix. - Assistant
The working tree already has an unrelated
package.jsonmodification, so I’ll avoid touching it. The likely path is theuseFormeffect that broadcasts values for unregister mode and the initialization/reset logic increateFormControl; I’ll inspect those sections and existingvaluestests. - Assistant
The reset implementation confirms the failure mechanism: with
shouldUnregister,_reset(..., { keepFieldsRef: true })writes incoming values into mounted fields and then immediately replaces_formValueswith{}. Since reactivevaluesuses exactly that reset mode, the first effect erases submission data. I’ll inspect the full reset function, registration behavior, and nearbyvaluestests to determine the narrowest semantics-preserving correction. - Assistant
A general change to reset ordering could affect explicit
reset()behavior, especially the contract that unregistered values are excluded undershouldUnregister. The safest fix is to preserve values only when reset is retaining field references (the internal reactive-valuespath), while still clearing values for ordinary resets. I’ll use blame/history to confirm intent, then create a temporary reproduction test outside the tracked test suite. - Assistant
Blame pinpoints the regression to the recent
keepFieldsReffeature: both the field-preservation loop and the clearing assignment were introduced together. I’ll inspect that commit’s rationale/tests and the prior code to ensure the fix aligns with intended mounted-field-only behavior rather than retaining unmounted incoming keys. - Assistant
The intended semantics are now clear: reactive
valuesmust retain only mounted fields whenshouldUnregisteris true. I’ll reproduce with both a mounted field and an unmounted key, asserting submission includes the former and excludes the latter. This will also guard against the tempting but incorrect fix of cloning the entirevaluesobject. The reproduction file is temporary and will be removed before submission. - Assistant
The temporary test reproduces the reported failure exactly: the mounted input displays the reactive value, but submission receives
{}. I’ll adjust_resetso that in thekeepFieldsRef + shouldUnregisterpath it initializes the allowed form-value base before callingsetValue; mounted fields are then repopulated, while ordinary reset behavior and unmounted-field exclusion remain unchanged. - Assistant
The edit did not apply because this environment lacks Python; the unchanged test appropriately still fails. I’ll apply the same targeted replacement using the repository editor command, then format and rerun.
- Assistant
I’ll now replace only lines 1662–1678, preserving the surrounding reset branches.
- Assistant
The reproduction now passes and confirms both required properties: mounted reactive data survives and unmounted reactive data is not submitted. I’ll remove the temporary test, then run the affected
useForm, reset, and watch suites plus TypeScript checking. These cover reactive values, reset option interactions, unregister lifecycle, and the changed internal type flow. - Assistant
All 130 targeted tests and TypeScript checks pass. I’ll now inspect the final diff for accidental changes, run lint on the modified source, and execute the full unit suite to catch interactions beyond the directly affected reset/watch paths. I’ll also ensure the pre-existing
package.jsonchange remains the only unrelated working-tree item. - Assistant
The full suite passes (118 suites, 1120 tests), and lint passes. However, running the installed Prettier version reformatted three unrelated union types in the same file. I’ll remove that noise by restoring the source and reapplying only the functional hunk without invoking Prettier, then rerun a focused validation and inspect the exact final diff.
- Assistant
I’ll reapply the verified functional change at the restored line numbers, with no whole-file formatting.
- Assistant
The focused command stopped at lint because the repository’s installed Prettier version flags three pre-existing formatting locations; importantly, it reports no issue in the changed hunk. Those are the same locations that
prettier --writealtered earlier, so I’ll leave them untouched to keep the patch focused. I’ll rerun the targeted tests independently, run type checking and whitespace validation, and inspect final status.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.