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
Interface
No new interfaces are introduced.
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.
- Assistant
I’ll first inspect the repository structure and locate the
values/shouldUnregisterinitialization and reset logic, along with relevant tests and project scripts. Then I’ll reproduce the failing behavior using the existing test setup before making a focused source change. - Assistant
The likely interaction is between
useForm’s reactivevaluesreset effect and the special render flush forshouldUnregister. I’ll inspect those effects and the control’s initialization/reset implementation, then search existingvaluestests to model an exact regression reproduction without adding repository tests. - Assistant
Initialization deliberately leaves
_formValuesempty undershouldUnregister, relying on mounted field registration to hydrate defaults. However, the reactivevalueseffect calls_reset(..., { keepFieldsRef: true }); reset then clears_names.mountand, becauseshouldUnregisteris true, replaces_formValueswith{}after setting mounted fields. This explains why a subsequent submit sees missing data. I’ll inspect registration and reset tests/history to determine the safest invariant—most likely retaining populated values specifically when field refs are retained. - Assistant
A recent related fix preserves reset values for conditionally mounted Controllers, so changing generic reset semantics risks regressions. I’ll compare the current code to v7.71.2 and inspect the exact historical changes around reset preservation. I’ll also create a temporary Jest regression test outside tracked test files (then remove it), matching the reported form submit scenario, to verify the failure concretely.
- Assistant
keepFieldsRefwas introduced specifically for reactivevalues. I’ll inspect that commit’s intended semantics and current reset registration behavior, then run the temporary test. - Assistant
The introducing commit confirms the defect: it calls
setValuefor retained mounted refs and immediately overwrites_formValueswith{}whenevershouldUnregisteris enabled. The correct narrow behavior is to preserve the mounted-field values just populated whenkeepFieldsRefis true, while retaining existing reset behavior otherwise. Before editing, I’ll run a temporary exact submit test to establish the failure. - Assistant
The reproduction fails exactly as reported: required validation receives no value and
onValidis never called. I’ll make the minimal reset change soshouldUnregisterpreserves_formValuesonly whenkeepFieldsRefis active (the reactivevaluespath); ordinary resets still clear values, andkeepDefaultValuesretains its existing precedence. - Assistant
Python is unavailable in this environment, so no source change occurred. I’ll apply the same focused edit using the repository’s editor command and inspect the diff.
- Assistant
The fix is now applied. I’ll rerun the same temporary regression test to confirm successful submission, then run the directly affected reset, watch, handleSubmit, and general useForm suites. I’ll also run formatting/type/lint checks afterward; if the conditional self-reference is rejected or suboptimal, I’ll refactor it without changing semantics.
- Assistant
The exact reproduction now passes: the submitted payload is
{ name: 'Bill' }and invalid submission is not invoked. I’ll run the relevant existing suites in one Jest invocation, covering reactive values, submit, reset, registration/unregistration, watch, and Controller behavior undershouldUnregister. - Assistant
All 237 targeted existing tests pass. The edge behavior remains constrained correctly: normal
shouldUnregisterresets still clear_formValues; only resets retaining field refs preserve the values populated for mounted fields;keepDefaultValuesstill takes precedence; and existing conditional Controller/reset tests pass. I’ll now run Prettier, ESLint on the modified file, and the full TypeScript check. - Assistant
TypeScript passes. Prettier/ESLint report three pre-existing formatting differences elsewhere in this file caused by the installed formatter version, while the modified region itself is not listed by ESLint. The file-level Prettier check still notices the missing separator line from the line-based edit, so I’ll restore that line manually, then verify our diff and rerun ESLint only against the changed line range if supported; I will not reformat unrelated existing code.
- Assistant
The source diff is now clean and the temporary test is removed.
git statusreveals an unexpectedpackage.jsonmodification, likely environmental/package-manager metadata from command execution; I’ll inspect it and revert only that unrelated change. Then I’ll run the full test suite for final confidence, since the targeted suite was fast. - Assistant
The full suite passes: 118 suites, 1,120 tests, and 8 snapshots. The console warnings are existing test warnings and did not fail any suite. I’ll perform the final repository hygiene check now: ensure only the intended source file changed, no temporary test remains, no whitespace errors exist, and review the final diff.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.