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 start by exploring the repository structure and understanding the issue with
valuesbeing reset whenshouldUnregisteris provided. - Assistant
Let me examine the issue more closely and find relevant code around
valuesandshouldUnregister. - Assistant
Let me look at useForm.ts and how values and shouldUnregister interact. Also let me check the test files and the codesandbox description more carefully.
- Assistant
Let me look at the codesandbox reproduction and relevant parts of createFormControl around values handling.
- Assistant
Let me open useForm.ts and createFormControl.ts around the relevant sections.
- Assistant
Let me read the rest of useForm.ts
- Assistant
Let me look at createFormControl for the initialization of _formValues when shouldUnregister is true, and the reset function.
- Assistant
Let me examine how registration and unmounting interact with
valueswhenshouldUnregisteris true. - Assistant
Let me look at the reset function and _removeUnmounted more carefully.
- Assistant
Let me look at the reset function around line 1620
- Assistant
Found a likely bug in
_reset— whenshouldUnregisteris true it clears_formValuesafter setting field values. Let me read the rest and write a reproduction. - Assistant
Let me check how handleSubmit collects values and look at related tests.
- Assistant
Let me check existing tests for values + shouldUnregister and write a reproduction.
- Assistant
Let me check git history for related fixes and look more carefully at the reproduction scenario.
- Assistant
Let me inspect the recent related fix and create a reproduction of the issue.
- Assistant
Let me write a small reproduction test based on the reported issue.
- Assistant
I'll put the repro test inside the project's test directory.
- Assistant
I've reproduced the bug — submit gets
{}. Let me inspectregisterand how_names.mountinteracts withkeepFieldsRef. - Assistant
So register always adds to
_names.mount. Since register is called during render (not just ref callback), when React re-renders after the values effect (updateFormState is called), fields get re-regi... - Assistant
Let me check the history of that
_formValuesassignment to understand the intended behavior. - Assistant
Let me check existing tests related to this combination, AND verify what happens after a simpler fix.
- Assistant
The bug is that
_resetclears_formValueswhenshouldUnregisteris true, even ifkeepFieldsRefalready applied values viasetValue. Fixing that now. - Assistant
The edit landed in the wrong file — I'll revert it and apply the fix to
createFormControl.ts. - Assistant
Good, now I can apply the fix to the correct file.
- Assistant
Running the reproduction test to verify the fix.
- Assistant
Reproduction passes. Running related existing tests next.
- Assistant
Let me expand the repro to cover edge cases like values updates and conditional fields.
- Assistant
All edge cases pass. Running a broader test suite, then removing the temporary repro file.
- Assistant
All tests passed. Cleaning up the temporary repro file and polishing the fix.
- Assistant
I'll simplify the fix so we skip reassigning
_formValueswhenkeepFieldsRefalready applied the values. - Assistant
Re-running the repro and related tests with the simplified fix.
- Assistant
Removing the temporary test and verifying the final diff.
- Assistant
Reverting the unintended
package.jsonchange.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.