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 locate relevant files. Let me first check the current directory.
- Assistant
Let me explore the source code structure and understand how
valuesandshouldUnregisterare handled. - Assistant
Let me first understand the issue better by looking at how
valuesandshouldUnregisterinteract in the source code. - Assistant
Now let me look at the
valuesprop and how it interacts withshouldUnregister. - Assistant
Let me look at the useForm.ts more closely to understand the flow with
valuesandshouldUnregister. - Assistant
Now let me look at
createFormControl.ts, especially around the_resetfunction and theshouldUnregisterlogic: - Assistant
Let me look at the key parts of createFormControl.ts, particularly around shouldUnregister and values handling.
- Assistant
Let me look at the
_resetfunction increateFormControl.tsto understand how values are handled during reset. - Assistant
Let me look at the _reset function and its behavior.
- Assistant
Now let me understand the issue better. Let me trace the flow more carefully, especially the
_resetfunction and the interaction withshouldUnregister. Let me also look at how_getWatchworks: - Assistant
Let me check the _getWatch function to see what values it returns:
- Assistant
Now let me understand how the
_resetfunction handles values whenshouldUnregisteris true. Let me look at the critical section: - Assistant
- Assistant
Let me now look at the specific code around line 158 to understand the initialization of
_formValues: - Assistant
Now I understand the flow. Let me see the full
_resetfunction more carefully: - Assistant
Now I understand the issue! Let me trace through the problem step by step:
- When
shouldUnregister: trueis set,_formValuesis initialized as{}(line 158). - When
props.valuesis passed and changes,_resetis called withkeepFieldsRef: true(line 155-158 of useForm.ts). - In the
_resetfunction, whenkeepFieldsRef: true, it iterates over mounted fields and callssetValuefor each field. ThesetValuefunction sets values in_formValuesvia theset(_formValues, name, ...)call. - However, after the
setValueloop, the code sets_formValuesagain at line 1677:
Since_formValues = _options.shouldUnregister ? keepStateOptions.keepDefaultValues ? (cloneObject(_defaultValues) as TFieldValues) : ({} as TFieldValues) : (cloneObject(values) as TFieldValues);shouldUnregisteris true andkeepDefaultValuesis not set, this resets_formValuesto{}.
So the fix should be: when
keepFieldsRefis true (meaning fields are being kept), we should not reset to when is true. - When
- Assistant
Let me trace through the bug carefully:
- When
shouldUnregister: trueandvaluesis passed via props: useForm.tsline 153-167 runs the useEffect forprops.values- It calls
control._reset(props.values, { keepFieldsRef: true, ...control._options.resetOptions }) - Inside
_reset,formValues= the props.values (not empty) keepStateOptions.keepDefaultValuesis NOT set (not in resetOptions)keepStateOptions.keepValuesis NOT set- So we enter the
if (!keepStateOptions.keepValues)block - Since
keepStateOptions.keepFieldsRefis true, we callsetValuefor each mounted field - Then at line 1677-1682:
_formValues = _options.shouldUnregister ? keepStateOptions.keepDefaultValues ? (cloneObject(_defaultValues) as TFieldValues) : ({} as TFieldValues) : (cloneObject(values) as TFieldValues);
- When
- Assistant
Let me look at the existing tests for
valuesandshouldUnregister: - Assistant
Let me look at existing tests for the
valuesprop: - Assistant
Let me look at the tests around line 2255 to see how
valuesis used withuseForm: - Assistant
Now I understand the issue! Let me trace through the problem:
- When
shouldUnregister: trueis set,_formValuesis initialized as{}(line 158 of createFormControl.ts) - When
props.valuesis passed,_resetis called withkeepFieldsRef: true(useForm.ts line 155) - In
_reset, whenkeepFieldsRef: true,setValueis called for each mounted field, which sets values in_formValuesviaset(_formValues, name, ...) - But then at line 1677-1682,
_formValuesis reset to{}because of the shouldUnregister check!
The code at line 1677-1682:
_formValues = _options.shouldUnregister ? keepStateOptions.keepDefaultValues ? (cloneObject(_defaultValues) as TFieldValues) : ({} as TFieldValues) : (cloneObject(values) as TFieldValues);When
shouldUnregisteris true andkeepDefaultValuesis not set (which is the case when usingvaluesprop),_formValuesbecomes{}.The fix should check if
formValuesis explicitly provided (not empty). If so, use those values in even when is true. - When
- Assistant
Now let me apply the fix. The issue is that
_formValuesis always set to{}whenshouldUnregisteris true, even when explicit values are provided. We need to preserve the values when they are explicitly provided: - Assistant
Now let me understand the full flow by looking at
_resetDefaultValuesand thehandleSubmitfunction: - Assistant
Let me look at the handleSubmit function:
- Assistant
Let me look at the handleSubmit function:
- Assistant
Now I see the full picture. In
handleSubmit, line 1526:let fieldValues = cloneObject(_formValues);So if
_formValuesis{}, the submit handler gets empty data.Now let me apply the fix:
- Assistant
Good! Now let me verify the fix works by running the existing tests:
- Assistant
Let me try running the tests differently:
- Assistant
Let me run the tests and wait for completion:
- Assistant
One test is failing. Let me look at the test that's failing:
- Assistant
The test fails because the fix now also affects
reset()behavior. The issue is that withshouldUnregister: true,reset()should only contain values for mounted fields. But thevaluesprop scenario is different - it useskeepFieldsRef: true.Let me update the fix to only preserve values when
keepFieldsRefis true (which is the case whenvaluesprop is used): - Assistant
Now let me run the tests again to see if the test passes:
- Assistant
All 118 test suites pass with 1120 tests. Now let me also create a small reproduction script to verify the fix resolves the original issue:
- Assistant
Let me create a more targeted test file that follows the repository's test conventions:
- Assistant
Let me create the test file inside the src directory instead.
- Assistant
Let me try using
--testPathPatternsinstead. - Assistant
The import path is wrong. Let me fix it.
- Assistant
The fix works! The reproduction test passes. Let me clean up the temporary test file and run the full test suite one more time to make sure everything passes:
- Assistant
All 118 test suites pass with 1120 tests. Let me review the change I made:
- Assistant
I see there's an unrelated change in
package.json. Let me revert that: - Assistant
Let me verify the final diff now.
- Assistant
Let me run the tests one final time to make sure everything still passes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.