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
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.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
- Assistant
I'll start by exploring the repository structure to understand the codebase and locate relevant files. Let's first see what's in the root directory.
- Assistant
Let me explore the repository structure more.
- Assistant
Let me understand the issue better. The issue is about
valuesdata being reset after first render whenshouldUnregisteris provided. Let me look at the source code to understand howvaluesandshouldUnregisterinteract.Let me first look at the main source files.
- Assistant
Let me look at the useForm.ts file as it's likely where the issue lies, and also check the useController and useFieldArray for references to
valuesandshouldUnregister. - Assistant
Let me examine the createFormControl function and understand the logic:
- Assistant
Let me look at the createFormControl.ts to understand the core logic.
- Assistant
Let me look at the _removeUnmounted function specifically:
- Assistant
Let me look at the _removeUnmounted function:
- Assistant
Let me check how _reset is called when values are provided with
shouldUnregister: - Assistant
Let me look at the _reset function:
- Assistant
Let me look at handleSubmit:
- Assistant
Let me look at the handleSubmit function:
- Assistant
Let me understand the issue better by examining the existing tests:
- Assistant
Let me look for existing tests related to
shouldUnregisterandvalues: - Assistant
Let me create a reproduction script first to understand the problem better:
- Assistant
Let me look at the issue in the codesandbox more closely and understand the problem. Let me trace through the code flow for
shouldUnregister: truewithvaluesprop: - Assistant
Let me look at how
_getWatchis used in handleSubmit: - Assistant
Let me look at the specific area in
_resetwhere_formValuesis set: - Assistant
Let me trace the issue. When
shouldUnregister: trueandvaluesprop is used:_formValuesstarts as{}becauseshouldUnregister: true(line 158)- When
props.valueseffect runs, it calls_resetwithkeepFieldsRef: true - In
_reset, line 1677-1681,_formValuesis set to{}again becauseshouldUnregisteris true andkeepDefaultValuesis not set - So
handleSubmitgets empty_formValues
Let me check how
_resetis called from thevalueseffect inuseForm.ts: - Assistant
Let me also look at what
_state.mountand_state.watchare set to after_reset: - Assistant
Now I understand the issue clearly. Let me trace through the problem:
- When
shouldUnregister: trueandvaluesprop is provided:_formValuesstarts as{}(line 158 in createFormControl)- When
valueseffect runs,_resetis called withkeepFieldsRef: true - But in
_reset(line 1677-1681),_formValuesis set to{}again becauseshouldUnregisteris true andkeepDefaultValuesis not set - So
handleSubmitreturns empty data
Let me look at what the existing tests say:
- When
- Assistant
Let me check if there are any existing tests for this specific scenario:
- Assistant
Let me look at the
registerfunction in createFormControl to understand how values flow: - Assistant
Let me look at how
_formValuesis being set and understand the exact flow forshouldUnregister+values: - Assistant
Let me understand the full flow. When
shouldUnregister: trueandvaluesis provided:_formValuesstarts as{}(line 158)- The
valueseffect calls_resetwithkeepFieldsRef: true - But in
_reset,_formValuesis set to{}again (line 1677-1681) becauseshouldUnregisteris true andkeepDefaultValuesis not set - So
handleSubmitsubmits empty data
Let me also check what
_state.mountand_state.watchare set to: - Assistant
Let me look at the exact flow. The issue is in
_reset- whenshouldUnregisteris true,_formValuesis set to{}. Let me also check howuseFormhandles thevaluesprop effect: - Assistant
Let me look at how
_getWatchworks and howhandleSubmitgets values: - Assistant
Let me look at how register works and how
_formValuesis populated: - Assistant
Now I understand the issue clearly. Let me trace through the problem:
- When
shouldUnregister: trueandvaluesprop is provided touseForm _formValuesstarts as{}(line 158 of createFormControl.ts)- The
valueseffect calls_reset(props.values, { keepFieldsRef: true }) - In
_reset,_formValuesis set to{}again (line 1677-1681) becauseshouldUnregisteris true andkeepDefaultValuesis not set handleSubmitusescloneObject(_formValues)which is empty{}
The fix should be in
_reset- whenshouldUnregisteris true butkeepFieldsRefis true (meaning we're resetting with values from thevaluesprop), we should preserve those values in_formValues.Let me first check how tests are run:
- When
- Assistant
Let me look at the test scripts:
- Assistant
Now let me write a reproduction script to confirm the bug:
- Assistant
Let me run this test to confirm the bug:
- Assistant
Let me create the test in the right location.
- Assistant
Let me check how other tests import:
- Assistant
Let me check how other tests import from the source:
- Assistant
Let me look at how the tests import the module:
- Assistant
Let me check how imports work in the test files.
- Assistant
Let me understand the full import structure.
- Assistant
Let me look for existing tests related to the values prop and shouldUnregister
- Assistant
Now I understand the issue. Let me look at the exact lines that need to change:
- Assistant
Now I understand the issue. Let me trace the flow:
- When
shouldUnregister: trueandvaluesis provided,_formValuesstarts as{}(line 158) - The
valueseffect calls_reset(props.values, { keepFieldsRef: true }) - In
_reset,_formValuesis set to{}again becauseshouldUnregisteris true andkeepDefaultValuesis not set (line 1677-1681) - So
handleSubmitsubmits empty data
The fix should be: when
keepFieldsRef: true(which indicates avaluesprop-driven reset), we should preserve the values in_formValueseven whenshouldUnregisteris true.Let me apply the fix:
- When
- Assistant
Now I understand the full issue. Let me trace the exact problem:
When
shouldUnregister: trueandvaluesis provided:_formValuesstarts as{}(line 158)- The
valueseffect calls_reset(props.values, { keepFieldsRef: true }) - In
_reset,_formValuesis set to{}again becauseshouldUnregisteris true andkeepDefaultValuesis not set (line 1677-1681) - So
handleSubmitsubmits empty data
The fix is to modify line 1677-1681 to also consider
keepFieldsRef. WhenkeepFieldsRefis true (used by thevaluesprop effect), we should preserve the values in_formValues: - Assistant
It seems the edit went wrong. Let me check the current state:
- Assistant
The edit went wrong - it removed the
_formValuesline. Let me fix this: - Assistant
Let me verify the file is restored:
- Assistant
Good, the file is restored. Now let me apply the fix correctly using sed:
- Assistant
Let me verify the change:
- Assistant
Good, the fix is applied. Now let me also check if there are similar issues with
_state.mountthat could affectwatch(): - Assistant
Now let me run the existing tests to ensure my change doesn't break anything:
- Assistant
Good, reset tests pass. Let me run the unregister tests:
- Assistant
Let me now also look at the
useForm.tsto see if there's also a problem there with theshouldUnregistereffect that sends state update: - Assistant
Let me run all the tests to make sure nothing is broken:
- Assistant
All existing tests pass! The only failure is my reproduction test which has a wrong import path. Let me fix it and create a proper test to verify the fix:
- Assistant
Let me also check if
handleSubmitneeds a fix. WhenshouldUnregister: true,handleSubmituses_formValues, but_state.mountis set to false, which means_getWatchwould use_defaultValues. However,handleSubmituses_formValuesdirectly: - Assistant
Now let me also check if there's a similar issue in
handleSubmitwhere it gets values from_formValues. The problem is: whenshouldUnregisteris true and_resetis called withkeepFieldsRef: true,_formValuesshould contain the values, but previously it was being reset to{}. My fix ensures_formValueshas the correct values whenkeepFieldsRefis true.Let me also look at the
handleSubmitto make sure it uses_formValuescorrectly: - Assistant
Now let me also check if the watch behavior is affected. When
shouldUnregister: true,_state.watchis set to true: - Assistant
Now let me write a proper test to verify the fix works. Let me look at how existing tests are structured:
- Assistant
Let me run this test to see if my fix resolves the issue:
- Assistant
The test passes! Now let me clean up the reproduction test and run the full test suite to make sure nothing else is broken:
- Assistant
Now let me run all the tests:
- Assistant
All 118 test suites pass with 1120 tests. Let me also look at the diff to confirm the change:
- Assistant
There's an unintended change to package.json. Let me revert that:
- Assistant
Let me also remove the temp file:
- Assistant
Let me verify the final diff:
- Assistant
The change is clean. Let me run the full test suite one more time to make sure everything passes with the final diff:
- Assistant
All 118 test suites pass with 1120 tests. Let me also verify there are no leftover test files:
- Assistant
Let me now do a final review of the fix and go through the checklist:
- Assistant
Now let me verify the fix once more by looking at the full context:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.