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 to understand the structure and locate relevant files. Let's first get an overview.
- Assistant
Let me explore the repository structure to understand the codebase.
- Assistant
Let me explore the src directory and understand the structure.
- Assistant
Let me explore the logic directory and understand the structure better.
- Assistant
Let me understand the issue better. The issue is about
valuesdata being reset after first render whenshouldUnregisteris provided. Let me look at the main form control logic. - Assistant
Let me look at the key areas. The issue is about
valuesdata being reset after first render whenshouldUnregister: trueis set. Let me look at howvaluesis handled in thecreateFormControl.tsfile, particularly around how_formValuesis initialized and updated whenvalueschanges. - Assistant
Let me look at the useForm.ts file to understand the issue better.
- Assistant
Let me look at the
_resetfunction increateFormControl.ts: - Assistant
Let me look at the
_resetfunction: - Assistant
Now let me look at the key part of
_reset: - Assistant
Now I understand the issue. Let me trace through the problem:
- When
useFormis called with bothvaluesandshouldUnregister: true, thevaluesprop provides data to the form. - The
_resetfunction is called withprops.valuesand{ keepFieldsRef: true }. - Inside
_reset, whenshouldUnregisteris true andkeepDefaultValuesis false,_formValuesis set to{}(empty object). - This happens AFTER the code already called
setValuefor each field in thekeepFieldsRefbranch.
The issue is that on line 1677-1680, when
shouldUnregisteris true andkeepDefaultValuesis false,_formValuesis always set to{}even though thevalueswere provided.Let me create a reproduction script first:
- When
- Assistant
Let me look at the actual test files related to this behavior:
- Assistant
Let me look at the
useForm.test.tsxfor tests related tovaluesandshouldUnregister: - Assistant
Let me look at the tests for
valuesprop specifically: - Assistant
Let me search for tests that test the
valuesprop in useForm: - Assistant
Let me look at the specific test cases around
valuesto understand the existing behavior: - Assistant
Now I understand the issue. Let me look more closely at what happens when
_resetis called withvaluesandkeepFieldsRef: true: - Assistant
Now I understand the issue. Let me trace through what happens:
- When
useFormis called withvaluesandshouldUnregister: true, the_formValuesis initialized to{}because of line 158:
let _formValues = _options.shouldUnregister ? ({} as TFieldValues) : cloneObject(_options.defaultValues || _options.values) || {};-
When the
useEffectruns withprops.values, it callscontrol._reset(props.values, { keepFieldsRef: true, ...control._options.resetOptions }). -
In the
_resetfunction, after processing values, line 1677-1680:
_formValues = _options.shouldUnregister ? keepStateOptions.keepDefaultValues ? (cloneObject(_defaultValues) as TFieldValues) : ({} as TFieldValues) : (cloneObject(values) as TFieldValues);When
shouldUnregisteris true andkeepDefaultValuesis not set,_formValuesis set to{}, which wipes out the values.The fix should be to also check for
keepFieldsRefwhenshouldUnregisteris true, so that when values are being provided via the prop, they are preserved. - When
- Assistant
Now I understand the issue clearly. Let me look at how the flow works:
- On initialization, when
shouldUnregister: true,_formValuesis set to{}(line 159) - When the
valuesuseEffect runs, it calls_reset(props.values, { keepFieldsRef: true, ... }) - In
_reset, the code iterates_names.mountand callssetValuefor each mounted field - But then at line 1677-1680, when
shouldUnregisteris true andkeepDefaultValuesis not set,_formValuesis set to{}- wiping out the values!
The problem is that when
shouldUnregisteris true andkeepFieldsRefis true (which is the case whenvaluesprop is used), the code still sets_formValuesto{}. ThekeepFieldsRefoption should preserve the values.Let me create a test to confirm the issue:
- On initialization, when
- Assistant
Let me check how tests are run.
- Assistant
Let me check the full scripts section and find the test command.
- Assistant
Let me run a test with pnpm:
- Assistant
Let me check the jest config:
- Assistant
Let me check the jest version to use the right command line options.
- Assistant
Let me try with the correct option name for jest 30:
- Assistant
Now I understand the issue. Let me trace the exact problem:
- When
useFormis called withvaluesandshouldUnregister: true,_formValuesis initialized to{}(line 158-159) - The
useEffectinuseForm.tscalls_reset(props.values, { keepFieldsRef: true, ...control._options.resetOptions }) - Inside
_reset, at lines 1677-1680, whenshouldUnregisteris true andkeepDefaultValuesis not set,_formValuesis set to{}even though values were provided andkeepFieldsRefis true
Let me look at the
_resetfunction more carefully: - When
- Assistant
Now I clearly see the problem. The issue is on lines 1677-1680:
_formValues = _options.shouldUnregister ? keepStateOptions.keepDefaultValues ? (cloneObject(_defaultValues) as TFieldValues) : ({} as TFieldValues) : (cloneObject(values) as TFieldValues);When
shouldUnregister: trueandkeepDefaultValuesis not set (butkeepFieldsRefis true - which is the case whenvaluesprop is used inuseForm.ts),_formValuesis set to{}which wipes out the values.The
useFormhook calls_resetwithkeepFieldsRef: truewhenvaluesprop is provided (line 155-156 inuseForm.ts):control._reset(props.values, { keepFieldsRef: true, ...control._options.resetOptions, });The fix should be to also preserve values when
keepFieldsRefis true andshouldUnregisteris true.Let me also check the initialization of
_formValues: - Assistant
Now I understand the complete picture. Let me make the fix:
- Assistant
Let me check what
setValuedoes with_formValues: - Assistant
Let me understand the
_setValuefunction and what it does with_formValues: - Assistant
Now I understand the issue completely. The problem is on line 1677-1680 in
createFormControl.ts:_formValues = _options.shouldUnregister ? keepStateOptions.keepDefaultValues ? (cloneObject(_defaultValues) as TFieldValues) : ({} as TFieldValues) : (cloneObject(values) as TFieldValues);When
shouldUnregisteris true andkeepFieldsRefis true (which is used whenvaluesprop is provided), the code sets_formValuesto{}instead of preserving the values. This is because it only checks forkeepDefaultValuesbut notkeepFieldsRef.Let me search for existing tests related to this scenario:
- Assistant
No test found. Let me search differently.
- Assistant
Let me look at the existing test that tests
shouldUnregister: true: - Assistant
Let me understand the flow better. Let me look at what happens in the
useEffectfor values: - Assistant
Now I understand the full flow. Let me create a reproduction script first:
- Assistant
Let me look at
handleSubmitto see how it reads the form values: - Assistant
Let me look at the handleSubmit function to understand how it reads form values:
- Assistant
Now I understand the issue completely. Let me trace through the code:
handleSubmit(line 1526) readscloneObject(_formValues)as the form values- When
shouldUnregister: trueand thevaluesprop is provided, the_resetfunction is called withkeepFieldsRef: true - In
_resetat line 1677-1680, whenshouldUnregisteris true andkeepDefaultValuesis false,_formValuesis set to{} - This means the form submits empty data even though
valueswas provided
The fix is to also check
keepFieldsRefin the condition, so that whenshouldUnregisteris true andkeepFieldsRefis true (used whenvaluesprop is provided), the values are preserved.Let me make the fix:
- Assistant
Let me navigate to the exact lines to edit:
- Assistant
Now I can see the exact issue. Line 1677-1681:
_formValues = _options.shouldUnregister ? keepStateOptions.keepDefaultValues ? (cloneObject(_defaultValues) as TFieldValues) : ({} as TFieldValues) : (cloneObject(values) as TFieldValues);When
shouldUnregister: trueandkeepFieldsRef: true(which is what thevaluesprop triggers),_formValuesgets set to{}because the condition only checkskeepDefaultValues.The fix should add
keepFieldsRefto the condition: - Assistant
Wait, I need to reconsider. The
keepDefaultValuescase is specifically different - whenkeepDefaultValuesis true, it should use_defaultValues. Let me refine the fix: - Assistant
Good. Now let me write a test to verify this fix resolves the issue:
- Assistant
Let me run the test to see if it passes:
- Assistant
I need to fix the import path. Let me look at how other test files import.
- Assistant
The test file is using relative imports to
../src/useForm. Let me check the correct import path: - Assistant
I see, the test files import from
../relative to the test file. Let me fix the test: - Assistant
Let me also check what imports are typically used in tests:
- Assistant
The test passes. Now let me run the full test suite to make sure we haven't broken anything:
- Assistant
All existing tests pass. Let me also run other test suites that might be affected:
- Assistant
Let me run the full test suite to ensure nothing is broken:
- Assistant
All 1121 tests pass. Now let me clean up the test file I created (the instructions say not to add new tests to the repository):
- Assistant
Let me also check the initialization of
_formValues. Let me look at lines 158-160: - Assistant
Now let me also look at the
_state.mountflag in the_resetfunction, as this is also affected byshouldUnregister:Let me also look at the
_state.mountflag, which is related to the issue: - Assistant
Let me check how
_state.mountis used inuseForm.ts: - Assistant
Let me also check how
_state.mountis used: - Assistant
Let me also check if the initial
_formValuesinitialization should be fixed too. WhenshouldUnregisteris true andvaluesis provided, the initial_formValuesis set to{}: - Assistant
The initialization of
_formValuesat line 158-160 also sets it to{}whenshouldUnregisteris true, even whenvaluesis provided. However, this is handled by theuseEffectinuseForm.tsthat calls_resetwith the values. The fix I made ensures that when_resetis called withkeepFieldsRef: true(as is the case whenvaluesprop is used), the form values are preserved.Let me verify the change is correct by looking at the diff:
- Assistant
I see there's an unrelated change to
package.json. Let me revert that: - Assistant
Good. Now let me verify the fix is correct by looking at the diff again:
- Assistant
The diff looks clean and correct. The fix is simple and targeted - it adds
keepStateOptions.keepFieldsRefto the condition for_formValuesassignment. WhenshouldUnregisteris true andkeepFieldsRefis true (which is the case whenvaluesprop is used inuseForm.ts),_formValuesis set tocloneObject(values)instead of{}.Let me run the full test suite one more time to make sure everything is good:
- Assistant
All 1120 tests pass. Let me verify the test file was removed:
- Assistant
Good. Let me verify the final state of the fix:
- Assistant
Let me verify the final change one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.