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 reproducing the issue described.
- Assistant
Let me examine the source code related to
valuesandshouldUnregister. - Assistant
Let me look at the useForm.ts and find related tests for values and shouldUnregister.
- Assistant
rg is not available, I'll use grep.
- Assistant
Let me examine how
valuesandshouldUnregisterinteract in the form control logic. - Assistant
Let me read the full useForm.ts file around these areas.
- Assistant
Let me read the rest of the file.
- Assistant
Let me search for tests and logic related to
valueswithshouldUnregister. - Assistant
There's a setValues.test.tsx - let me look at that and also search github issue if there's something in changelog.
- Assistant
Let me search the codebase and CHANGELOG for related history on this bug.
- Assistant
Let me examine the createFormControl reset and removeUnmounted logic more closely.
- Assistant
Let me examine the reset function carefully.
- Assistant
Found a likely bug: after setting field values with
keepFieldsRef,_formValuesis wiped whenshouldUnregisteris true. Let me verify. - Assistant
Let me examine register, handleSubmit, and how values interact with shouldUnregister.
- Assistant
Let me read register, unregister, and handleSubmit carefully.
- Assistant
Let me also look at updateValidAndValue and handleSubmit, and
_removeUnmounted - Assistant
Let me read updateValidAndValue and handleSubmit
- Assistant
Let me look at handleSubmit and try to write a reproduction test.
- Assistant
I'll write a quick reproduction script to confirm the bug.
- Assistant
Need to fix the import path.
- Assistant
Let me check getValues implementation and the flow after values effect.
- Assistant
Let me check git history for
keepFieldsRefand related fixes. - Assistant
Let me also check
_removeUnmountedand existing tests for this scenario. - Assistant
Let me check existing related tests and PR notes for the intended behavior.
- Assistant
Let me read those tests around the relevant shouldUnregister cases.
- Assistant
Interesting - for normal reset with shouldUnregister and no registered fields, submit returns {}. If there are registered fields, look at controller test further.
- Assistant
Let me also search for tests with values prop + shouldUnregister.
- Assistant
Let me also check how useController handles this path and look at the recent "Preserve reset values" fix.
- Assistant
There's already a related fix for Controller - let me inspect it.
- Assistant
Let me check when Controller/useController registers fields.
- Assistant
I've confirmed the bug. Implementing the fix: when
keepFieldsRefis true, preserve form values instead of wiping them forshouldUnregister. - Assistant
I'll also preserve mount when keepFieldsRef since fields stay. Let me think once more...
- Assistant
I'll apply the fix for
_formValuesand also preserve mount set whenkeepFieldsRefis set. - Assistant
I'll also preserve mounted field names when
keepFieldsRefis true. - Assistant
Now let me run the reproduction test to verify the fix.
- Assistant
The fix works. Let me cover more edge cases and run the existing related test suites.
- Assistant
All those passed. Running broader related test suites next.
- Assistant
Reconsidering the mount change — it may incorrectly keep unmounted fields. Reverting it and keeping only the form values fix.
- Assistant
Removing the temporary test file and re-running related tests.
- Assistant
Re-verifying the fix once more, then reviewing the final diff.
- Assistant
There's an unintended package.json change — reverting it.
- Assistant
Checking whether package-lock.json was modified.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.