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 first inspect the repository structure, package scripts, and the form-control implementation around
valuesandshouldUnregister. Then I’ll identify or reproduce the reset path before making a focused source change. - Assistant
The key paths are the initial
_formValuesconstruction, the reactivevaluesreset inuseForm, and ashouldUnregistersynchronization effect. I’ll read those sections together with existingvaluestests and recent git history to understand the intended invariants. - Assistant
The likely failure is now concrete:
valuesis copied into_defaultValues, but_formValuesis initialized to{}solely becauseshouldUnregisteris true. Registration may repopulate mounted fields, but the reactivevalueseffect then calls_reset;_resetwrites mounted fields and immediately replaces_formValueswith{}again. I’ll inspect registration/reset in full and compare historical changes or upstream fixes before choosing the narrowest correction. - Assistant
_resetconfirms the destructive ordering: withkeepFieldsRef, it sets each mounted field into_formValues, then overwrites_formValueswith{}. Since thevalueseffect deliberately useskeepFieldsRef, this makes the synchronization self-defeating. Before editing, I’ll inspectregisterand reset-related tests to ensure preserving only mounted values does not violate explicit-reset semantics for unregistered fields. - Assistant
The regression originates in the
keepFieldsReffeature: it added both the values-prop reset option and the unconditional{}assignment undershouldUnregister. I’ll create a temporary (later removed) Jest regression test matching the report—controlledvalues, a registered required input, and submit—to verify the exact failure and then use it to validate the fix. - Assistant
The temporary test reproduces the report: the DOM receives
"Bill", but submit takes the invalid path because internal_formValuesis empty. I’ll change_resetso that when bothshouldUnregisterandkeepFieldsRefare active, it rebuilds_formValuesfrom the currently mounted field names aftersetValuehas applied conversion rules. This preserves registered values without leaking unregistered keys from the externalvaluesobject. - Assistant
The environment lacks Python, so that edit did not apply and the repeated failure is expected. I’ll apply the same targeted replacement with Node, confirm the diff, and rerun the regression test.
- Assistant
The focused regression now passes. I’ll strengthen the temporary verification for two important semantics: reactive updates to
valuesmust remain submittable, and keys present invaluesbut not registered must still be omitted undershouldUnregister. Then I’ll remove the temporary test and run the existing reset/useForm suites plus type and formatting checks. - Assistant
Both initial and updated external values submit correctly, while the unregistered key remains omitted; the temporary test has been removed. The existing
useFormand reset suites pass (109 tests). I’ll now run TypeScript, Prettier, targeted ESLint, and the handle-submit/controller suites because they exercise the altered internal value store. - Assistant
TypeScript passed, but the chained command stopped at Prettier, which requested formatting (so ESLint and the additional tests did not yet run). I’ll let the repository formatter normalize the changed file, inspect its exact diff, and rerun all remaining checks.
- Assistant
The functional checks passed (53 additional tests), but running the newer installed Prettier rewrote three unrelated pre-existing union layouts. To keep the patch focused, I’ll restore the file from HEAD and reapply only the functional hunk, then rerun
git diff --check, TypeScript, and targeted ESLint (without whole-file autoformatting). - Assistant
TypeScript passed again. ESLint reports only three baseline Prettier discrepancies unrelated to the changed region; the new block itself has no lint finding. I also noticed
package.jsonwas modified by tooling (likely the package-manager invocation), so I’ll inspect and revert that unrelated change, verify there are no temporary files, and run the full test suite for final confidence. - Assistant
The complete suite passes: 118 suites, 1,120 tests, and 8 snapshots. The console warnings shown are pre-existing test warnings; there are no failures. I’ll perform the final repository hygiene check (
git status, diff, whitespace, and no temporary tests), then submit the single-source-file fix.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.