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 the relevant tests and reproduce the reported submission behavior before changing source code. - Assistant
The likely interaction is in
useForm’s post-render effects: one effect resets whenprops.valueschanges, while another emits values forshouldUnregister. I’ll read those sections and the control’s initialization/reset implementation, plus existingvaluestests, to determine exactly where data is being cleared. - Assistant
Initialization deliberately starts
_formValuesempty undershouldUnregister, relying on registered fields to populate from_defaultValues. However, thevalueseffect calls_resetafter the first render;_resetwrites registered fields viasetValue, then immediately replaces_formValueswith{}because unregistering is enabled. That explains why visible inputs can retain values while submission receives missing data. I’ll inspect the full reset/register paths and repository history (including remote refs) for intended semantics before implementing the narrow fix. - Assistant
There is a recent related commit in the repository history, so I’ll inspect that change and blame the reset line. I’ll also check the unexpected
package.jsonworking-tree difference to ensure I do not overwrite unrelated pre-existing changes. - Assistant
The reset implementation added
keepFieldsRefspecifically for reactivevalues, but its final assignment discards the_formValuesthat the precedingsetValueloop just populated. The correct scope is to preserve only registered fields—not clone all incoming fields, which would violateshouldUnregister. I’ll verifysetValuebehavior for undefined/missing values and inspect the commit that introducedkeepFieldsRef, then reproduce with a temporary test file outside the tracked test suite. - Assistant
The introducing commit confirms this is an unintended interaction:
keepFieldsRefupdates mounted fields, then legacyshouldUnregisterreset logic clears them. I’ll now create a temporary Jest regression test (not undersrc, so it will be removed after verification) matching the report: reactivevalues,shouldUnregister: true, a registered required input, and form submission. - Assistant
Jest’s configured roots exclude a root-level temporary test, so the first reproduction attempt did not execute (and the file was removed). I’ll use a temporary Jest config pointing at
/tmpwhile reusing the repository’s SWC transform and jsdom environment, allowing reproduction without adding or modifying any repository test. - Assistant
The temporary config referenced the setup filename incorrectly (
setup.ts, notjest.setup.ts). I’ll correct that and usetestRegex/an explicit path so Jest accepts the test outside its normal tracked roots. - Assistant
Jest still enforces
rootDirfor explicit paths. I’ll make/tmpthe temporary root and reference all repository setup, transformer, and source paths absolutely; this keeps the repository untouched while allowing the test to run. - Assistant
The reproduction now fails exactly as reported: the input displays “Bill,” but submission calls the invalid handler because internal form values were cleared. I’ll change reset’s
shouldUnregisterbranch sokeepFieldsRefretains the mounted-field-only_formValuesbuilt by thesetValueloop. Ordinaryreset()behavior remains unchanged, and unregistered incoming keys are still excluded. - Assistant
The environment has no
python3, so the source edit did not occur; formatting was a no-op and the expected reproduction remained failing. I’ll apply the same narrow edit with the repository’seditcommand and rerun the temporary regression. - Assistant
The editor line range was based on the earlier
sednumbering, butopenshowed the actual current range shifted by three lines; this produced a duplicated partial expression and removed the array notification opener. I’ll repair the entire small block atomically, review it, then format and rerun. - Assistant
The regression now passes. Before finalizing, I’ll validate edge semantics with temporary tests: reactive
valuesupdates should submit the latest mounted value, and keys without registered fields must remain excluded undershouldUnregister. I’ll also run the existinguseForm,reset, andwatchsuites because the change is in shared reset logic. - Assistant
Both added edge checks pass, including exclusion of unregistered keys, and all 130 targeted existing tests pass. I’ll now run TypeScript checking and lint the changed source, inspect the final diff, and remove temporary files. The
package.jsonchange predated my source edit and is an environment-only dependency addition unrelated to this issue, so I’ll restore it to keep the submission focused. - Assistant
Type checking and ESLint pass, but running the current Prettier version also reformatted three unrelated union types. I’ll revert the source file and reapply only the intended assignment change without whole-file formatting, then rerun the checks and targeted regression suites to ensure the clean diff behaves identically.
- Assistant
The final diff is now only four lines. Type checking passes. Full-file ESLint reports three pre-existing Prettier-version disagreements on untouched lines (the earlier formatter run demonstrated them); the changed block itself has no lint issue. I’ll run ESLint with only that formatting integration disabled to validate all substantive rules, rerun the targeted suites after the clean reapplication, and perform final repository checks.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.