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, then identify relevant tests and reproduce the regression before editing source code. - Assistant
The relevant path is
useFormreacting toprops.valuesby calling_reset, whose form-value reconstruction differs undershouldUnregister. I also see an existingpackage.jsonmodification, so I’ll inspect that diff to avoid disturbing unrelated work, then read the exact initialization/reset effects and nearby tests for externally suppliedvalues. - Assistant
The initial control deliberately starts
_formValuesempty for unregistering forms, expecting mounted field registration to populate values. The regression likely occurs because the firstvalueseffect resets after registration, and_resetagain empties_formValueswhenshouldUnregisteris true even though explicit reactive values were supplied. I’ll inspect the full_resetimplementation, registration behavior, and value-focused test section to verify that lifecycle. - Assistant
The lifecycle confirms the precise failure: render-time registration copies the initial
valuesinto_formValues; afterward thevalueseffect treats those same initial values as a change because_values.currentstarts asundefined, calls_reset, writes mounted inputs, and then clears_formValuesdue to unregister semantics. The likely targeted fix is to seed the comparison ref with the initialprops.values, avoiding a redundant first-render reset while preserving resets for later prop changes. Before changing it, I’ll reproduce this with a temporary Jest test (outside tracked test files, removed afterward), and review this line’s history for intent. - Assistant
History is especially informative: the ref was previously seeded with
props.values, then deliberately reverted toundefinedin a later change. I need understand that revert’s purpose so the fix doesn’t reintroduce the memoized-controller bug. The temporary test wasn’t discovered because of Jest’s configured roots/matcher; I’ll inspect configuration and the two commits, then rerun the reproduction from the expected temporary location and remove it immediately. - Assistant
Seeding the ref fixed memoized
useController, but was reverted because somevaluesscenarios relied on the post-mount reset to populate fields. A blanket restoration is therefore unsafe. The robust fix should retain the initial effect but prevent_resetfrom clearing the explicit supplied values when it is invoked by reactivevalues. I’ll inspect the revert report’s regression test andkeepFieldsRefsemantics, then reproduce the current issue under Jest’ssrc/__tests__matcher. - Assistant
The reproduction fails exactly as reported: the visible input still says “Bill,” but submission is blocked because internal form data was cleared. The best-scoped fix is in
_reset: whenkeepFieldsRefkeeps mounted registrations, an unregistering form must also retain the values just assigned to those mounted fields. Cloning current_formValuespreserves only registered/mounted data (so unmounted keys still obeyshouldUnregister) rather than cloning every supplied key. I’ll implement that narrow branch, then rerun the reproduction plus add temporary checks for an unregistered supplied key and a later reactive values update. - Assistant
The edit did not apply because Python is unavailable in this environment, so the unchanged code predictably still fails. I’ll apply the same targeted change with the repository editor and rerun the temporary test.
- Assistant
The targeted reproduction now passes. The branch retains only
_formValuesalready maintained by mounted registrations, so supplied-but-unregistered keys remain excluded. I’ll now verify the two important lifecycle edges explicitly—reactive updates and conditional unmounting—and then run formatting/type checks and the affected existing suites (useForm, reset, registration/controller, and submit behavior). - Assistant
Both edge-case checks pass: later
valuesupdates submit the new mounted value, and after unmount the value is removed as required byshouldUnregister; TypeScript and formatting also pass. I’ll run the affected existing test suites now. - Assistant
All 211 tests in the five affected suites pass. I’ll perform the final hygiene check now: lint the changed source, inspect the final diff/status for temporary files, and remove the unrelated pre-existing
package.jsondependency change from the submission so only the bug fix is included. - Assistant
The installed Prettier version reformatted several unrelated type declarations in the same file. I’ll revert those formatting-only changes by restoring the file and reapply only the four-line behavioral branch, preserving the repository’s existing formatting, then rerun the focused test/type/lint checks against the exact final diff.
- Assistant
The behavioral diff is now isolated, but the editor consumed one existing blank line. I’ll restore that line to keep the patch minimal, then rerun the original reproduction and static checks against this exact final source.
- Assistant
The exact final source passes the regression and TypeScript. Full-file ESLint reports three pre-existing Prettier-version mismatches at lines 1114, 1188, and 1525, all unrelated to this diff; the changed branch itself has no lint finding. I’ll run ESLint with only the mismatched formatting rule disabled to confirm all semantic lint rules pass, then submit.
Checklist:
- Reproduced the reported error: a temporary test showed the input visibly contained
Bill, but submit never invoked the valid callback because internal values were empty. - Confirmed the fix resolves it: the same temporary test now submits
{ name: 'Bill' }. - Considered edge cases: temporary coverage verified later reactive
valuesupdates, excluded supplied-but-unregistered keys, and conditional unmount cleanup undershouldUnregister. - Ran affected existing tests: 211 tests across , , , , and all passed; TypeScript also passed. Full ESLint’s only errors are unrelated baseline formatting mismatches from the installed Prettier version.
- Reproduced the reported error: a temporary test showed the input visibly contained
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.