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 to understand the codebase and then find the relevant code related to the issue.
Assistant
Let me explore the source directory.
Assistant
The main logic is in
createFormControl.ts. Let me search for relevant code related toshouldUnregisterandvalues.Assistant
Let me look at the initialization and the relevant sections of the code.
Assistant
So the initialization is:
_defaultValues= cloneObject of defaultValues or values_formValues= shouldUnregister ? {} : cloneObject(_defaultValues)
So when
shouldUnregister: true,_formValuesstarts as{}.Now let me look at how the form mounts and resets values. Let me look at the section around line 1677, the reset function, and where
valuesoption is processed.Let me look at the
useForm.tsto understand howvaluesis being used.Assistant
Now I see key parts. There's a
React.useEffectthat listens toprops.shouldUnregisterand aprops.valueseffect. Let me look at the_resetfunction increateFormControl.ts.Assistant
Now let me look at the register function and how
_formValuesgets populated when fields mount.Assistant
Let me look at the
registerfunction.Assistant
Let me look at the register function specifically.
Assistant
Let me look at the
_getWatchand_removeUnmountedfunctions.Assistant
Let me look at the
_resetDefaultValuesfunction.Assistant
Now let me look at existing tests related to
valuesandshouldUnregisterto understand the test setup.Assistant
Let me look at the package.json test scripts.
Assistant
Let me look at an existing test to understand the test structure.
Assistant
Now let me write a reproduction test that mirrors the issue: providing
valuesandshouldUnregister: true, then submitting the form.Assistant
I've reproduced the issue. The submit data is
{}. Now let me look at thesetValuefunction to understand the full flow.Assistant
I've confirmed the issue. Let me look at the precise indentation of the
_resetfunction to understand the structure.Assistant
Let me check the git history of this part of the code to understand the intent.
Assistant
Let me look at the blame for the specific lines that reset
_formValues.Assistant
Let me look at the version before the
keepFieldsRefcommit to understand the prior behavior.Assistant
Let me look at the
getValuesandhandleSubmitfunctions to understand what gets submitted.Assistant
So
getValues()returns{ ..._formValues }(when mounted). So if_formValues = {}, it returns{}.Now let me look at
handleSubmitto confirm it usesgetValues().Assistant
handleSubmitusescloneObject(_formValues)directly. So if_formValues = {}, submit data is{}.Now let me check all callers of
_resetwithkeepFieldsRefto make sure I understand the scope.Assistant
keepFieldsRef: trueis only used inuseForm.ts(theprops.valueseffect). So the fix only affects that path. Let me view the exact lines to edit.Assistant
Now I understand the root cause. When
shouldUnregister: trueand_resetis called withkeepFieldsRef: true(from theprops.valueseffect), thesetValueloop populates_formValueswith the mounted fields' values, but then the line_formValues = {}wipes them all out.Let me apply the fix to preserve
_formValueswhenkeepFieldsRefis true:Assistant
Let me use the
editcommand instead.Assistant
Now let me run my reproduction test again to confirm the fix.
Assistant
The fix works. Now let me test some edge cases - dynamic values change, and the shouldUnregister behavior (only mounted fields submitted).
Assistant
All edge cases pass, including the shouldUnregister behavior of only submitting mounted fields. Now let me run the existing test suite to make sure I didn't break anything.
Assistant
All 660 tests pass. Let me run the full test suite to be thorough.
Assistant
All 1123 tests pass. Now let me clean up the temporary test file before submitting.
Assistant
Wait,
package.jsonis modified unexpectedly. Let me check what changed.Assistant
This
package.jsonchange is an unintended artifact (probably from an auto-install). Let me revert it to keep only my intended fix.Assistant
The diff now only contains my intended fix. Let me run a final verification that tests still pass after reverting
package.json.Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.