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
Let me start by exploring the repository structure to understand the codebase.
Assistant
The main logic is likely in
createFormControl.ts. Let me look at howvaluesandshouldUnregisterinteract.Assistant
Let me look at the
resetfunction in detail, since that's wherevaluesis used:Assistant
Now let me look at
useForm.tsto understand howvaluesis initialized and how reset is triggered:Assistant
Let me look at the git history of the
_resetfunction and theshouldUnregisterlogic:Assistant
Let me look at the
keepFieldsRefbranch and the_formValuesassignment more carefully. The issue is aboutvalues+shouldUnregister. Let me write a reproduction test first.Let me look at the existing tests for
valuesandshouldUnregistertogether.Assistant
Let me write a reproduction test to confirm the issue:
Assistant
There's a parse error. The
expect.anything()might not be supported in TS. Let me fix the test.Assistant
Let me check how existing tests are run and the jest config:
Assistant
I've reproduced the issue. The submit is called with
{}instead of{ test: 'test' }. Let me trace the register flow:Assistant
Let me look at
updateValidAndValue:Assistant
Let me look at
_removeUnmounted:Assistant
Let me look at the initialization of
_formValuesand_defaultValues:Assistant
Let me look at
handleSubmitand how it gathers values:Assistant
So
fieldValues = cloneObject(_formValues). With_formValues = {}, the submit gives{}.But wait, in
executeBuiltInValidation, let me check if it re-gathers values from fields. Let me look at that.Assistant
Let me look at
setValue:Assistant
Let me look at
_setValue:Assistant
Let me check the git history of this
_formValuesreset logic:Assistant
Let me check
cloneObjectbehavior:Assistant
Let me check the
unregisterfunction to confirm unmounted fields are removed from_formValues:Assistant
Now I understand the bug. Let me look at the exact lines to edit:
Assistant
Now I understand the bug. The
keepFieldsRefbranch callssetValuefor each mounted field (populating_formValuesin place), but then the subsequent_formValuesassignment wipes it to{}whenshouldUnregisteristrueandkeepDefaultValuesisfalse. SincekeepFieldsRefkeeps the fields mounted (they won't re-register to repopulate), the values are lost.Let me apply the fix:
Assistant
I'll use the
editcommand instead. Let me open the file and apply the fix:Assistant
Now let me run the reproduction test again:
Assistant
The reproduction test now passes. Let me add edge case tests to my repro to verify correctness:
Assistant
All edge cases pass. Now let me run the existing test suites to make sure I didn't break anything:
Assistant
Let me run the broader test suite (useController, useFieldArray, reset-related, shouldUnregister tests):
Assistant
All 1120 tests pass. Let me also check the reset-specific test file:
Assistant
Let me run the unregister and controller tests specifically (most relevant to
shouldUnregister):Assistant
Let me verify the type tests still pass:
Assistant
Let me check whether there are existing tests covering
keepDefaultValues+shouldUnregister+valuescombinations:Assistant
Let me test the
keepDefaultValues+shouldUnregister+valuesedge case with my current fix to decide whether to also handle it:Assistant
As I suspected, the
keepDefaultValues+shouldUnregister+valuescombination is also buggy. Let me see what it returned:Assistant
The
keepDefaultValuescase returns the old defaultAlexinstead of thevaluesJohn. Let me restructure the fix to handlekeepFieldsRefuniformly, so the values are applied regardless ofkeepDefaultValues:Assistant
Let me run the edge case test again:
Assistant
Now let me re-add the comprehensive repro tests and run them along with the full suite:
Assistant
Now let me remove the temporary repro tests and run the full existing test suite to confirm nothing broke:
Assistant
All 1120 tests pass. Let me run the type test once more and verify the final diff:
Assistant
There's an unintended change to
package.json(adependenciesblock was added, not by me). Let me revert it:Assistant
The diff is now clean — only
src/logic/createFormControl.tsis modified. Let me verify the final change in context:Assistant
Let me run the full test suite one final time with the final version of the fix to be completely sure:
Assistant
All 1120 tests pass with the final fix. Let me verify no temporary test files remain:
Assistant
The fix is complete and only
src/logic/createFormControl.tsis modified, with no leftover temporary files.Checklist review
-
Reproduced the error: ✅ I wrote a reproduction test (
useForm({ values: { test: 'test' }, shouldUnregister: true })then submitting) and confirmed it returned instead of .
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.