It is possible to successfully submit a form, when data is provided via values, and shouldUnregister: true option is set
What 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 using git reset --hard command.
Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later.
Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands.
However you don't need to use git add or git commit before submitting your changes. In fact,
submit only submits currently unstaged changes because it uses git diff to 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
Now I see how _formValues is initialized. Let me look at the lines around 1677 (where _formValues = _options.shouldUnregister ? ... is set).
Assistant
Now let me look at the broader context to understand what function this is in:
Assistant
Let me look at the broader context of where _formValues is used and set to understand the issue. Let me also look at the codesandbox URL from the issue to see how it's being reproduced:
Assistant
Now let me look at the unregister function to understand what happens when a field is unregistered:
Assistant
Now let me look at updateValidAndValue function:
Assistant
Now let me look at the codesandbox URL to understand the issue more precisely. Let me find similar test cases:
Assistant
Let me look at useForm.test.tsx to find related tests:
Assistant
Now let me reproduce the issue first. Let me check the Codesandbox example to understand what is being tested:
Assistant
Now let me look at where the issue could be. Let me try to reproduce the issue first by looking at the codesandbox example. Based on the description, the issue is:
values is provided (not defaultValues)
shouldUnregister: true is set
After the first render, values are reset
Let me look at how values is handled in the form:
Assistant
Now let me look at _removeUnmounted:
Assistant
Now let me look at the unregister function more carefully:
Assistant
Now let me check git log for recent changes related to the issue:
Assistant
Let me look at the recent changes that might be related to the issue:
Assistant
Let me look at the test that exists in the codesandbox. Let me try to first reproduce the issue:
Assistant
Let me check the jest command, maybe I need to use the config:
Assistant
I've successfully reproduced the bug. The submitted data is {} instead of the expected { firstName: 'initial', lastName: 'value' }.
Now let me analyze the issue. Looking at the flow:
Component renders with shouldUnregister: true and values: { firstName, lastName }
_formValues is initialized to {} (because of shouldUnregister: true)
_defaultValues is initialized with the values (cloned)
The register('firstName') is called, which calls updateValidAndValue(name, true, options.value). Since the value is undefined and we look up _formValues first which is {}, then _defaultValues, we get initial.
set(_formValues, name, defaultValue) - this sets _formValues.firstName = 'initial'.
Assistant
OK so _resetDefaultValues is only for async default values.
Now, let me trace the issue more carefully. The flow:
_formValues = _options.shouldUnregister
? keepStateOptions.keepDefaultValues
? (cloneObject(_defaultValues) as TFieldValues)
: ({} as TFieldValues) // <-- THIS IS THE BUG!
: (cloneObject(values) as TFieldValues);
When shouldUnregister: true and keepDefaultValues is not set, _formValues is reset to {} even though we just set values via setValue. This is the root cause of the issue!
The setValue calls above (for keepFieldsRef: true) were updating _formValues, but then this line overwrites _formValues with {}.
Let me look at git blame on this section to understand the history:
Assistant
OK so I see the history. The bug is in this code:
js
_formValues = _options.shouldUnregister
? keepStateOptions.keepDefaultValues
? (cloneObject(_defaultValues) as TFieldValues)
: ({} as TFieldValues)
: (cloneObject(values) as TFieldValues);
When shouldUnregister: true and keepDefaultValues: false, _formValues is reset to {}. But before this line, the code already calls setValue for each mounted field to set the values:
js
if (keepStateOptions.keepFieldsRef) {
for (const fieldName of _names.mount) {
setValue(
fieldName as FieldPath<TFieldValues>,
get(values, fieldName),
);
}
}
These setValue calls correctly set _formValues with the values, but then _formValues is reset to {}, throwing away all the work.
Let me look more carefully at the code:
Assistant
Now let me check the test case from the original PR #12923 that introduced the keepFieldsRef option:
Assistant
Now I understand. The PR #12923 introduced keepFieldsRef and made _formValues reset to {} when shouldUnregister: true. But this is incorrect because if keepFieldsRef: true is set (as is the case when values prop is used), the setValue calls populate _formValues, but then the line at 1677-1681 overrides it to {}.
Let me look at the related PR commit 879bb12b - "🐞 fix: rreserve reset values for conditionally mounted Controller fields with shouldUnregister":
Assistant
Assistant
Assistant
Now let me look at what the test case for keepFieldsRef looks like:
Assistant
So keepFieldsRef: true is only used by useForm.ts when props.values is provided. Let me look at the related test cases:
Assistant
Let me look at the test in useForm/reset.test.tsx:
Assistant
Let me look at the tests around line 992:
Assistant
OK, now let me check the should keep reset value for conditionally mounted controlled fields with shouldUnregister test - this is the test from PR #13423:
Assistant
OK let me now think about the best fix. Looking at the code:
js
_formValues = _options.shouldUnregister
? keepStateOptions.keepDefaultValues
? (cloneObject(_defaultValues) as TFieldValues)
: ({} as TFieldValues)
: (cloneObject(values) as TFieldValues);
The bug is that when keepFieldsRef: true is used and shouldUnregister: true and keepDefaultValues is not set, _formValues is reset to {}, losing the values set by setValue calls in the keepFieldsRef loop.
The fix should preserve _formValues (which now has the right values from the setValue calls) when keepFieldsRef is used.
Let me think about all the cases:
shouldUnregister: false: _formValues = cloneObject(values) - always populates with all values
Assistant
Assistant
Let me think about this even more carefully. The setValue calls in the keepFieldsRef: true branch happen inside an if (!keepStateOptions.keepValues) block, which is the path that resets the form.
Now, the question is: when keepFieldsRef: true is used, what should _formValues be set to?
Looking at the original logic (before PR #12923):
js
for (const fieldName of _names.mount) {
const value = get(values, fieldName, get(_defaultValues, fieldName));
if (!isUndefined(value)) {
set(values, fieldName, value);
setValue(
fieldName as FieldPath<TFieldValues>,
get(values, fieldName),
);
}
}
// ...
_formValues = cloneObject(values) as TFieldValues;
Assistant
OK so _setValue does set(_formValues, name, cloneValue). So after setValue calls for mounted fields, _formValues has the correct state.
But wait, I need to consider another case. What if _formValues had stale values from previous mounted fields that have been unregistered? For example:
Let me also verify the values prop is being passed correctly through useForm:
Assistant
All my tests pass! The fix correctly handles:
✓ Initial values from values prop are preserved
✓ Updated values are correctly applied
✓ Unmounted field values are correctly removed (shouldUnregister behavior)
Let me clean up the test file and also run all the tests to be sure:
Assistant
Now let me check that the test for the should keep reset value for conditionally mounted controlled fields with shouldUnregister test still works. That test from PR #13423 is in reset.test.tsx. Let me see what happens in that test:
Assistant
All tests pass. Now let me also run the full test suite to make sure no other tests are affected:
Assistant
All 1120 tests pass. Let me also check my changes with git diff:
Assistant
The fix is minimal and focused. Let me also think about other edge cases I might have missed.
Let me also check if there are any issues with the keepDirtyValues: true case:
Assistant
All edge cases pass. Let me clean up:
Assistant
Now let me look at the diff once more and verify the change:
Assistant
There's a package.json change I didn't make. Let me check:
Assistant
The package.json change seems to be a pre-existing change, but it's not from me. Let me revert that change:
Assistant
Now the only change is the fix in createFormControl.ts.
Let me now also think about whether my fix might have any unintended side effects. Let me look at the related keepFieldsRef block once more:
Assistant
Now let me think more about edge cases. The _formValues is used in _setValue, _getDirty, _getWatch, etc. After the fix, when keepFieldsRef: true is used, _formValues retains its state from before the assignment. This is correct because:
setValue was called for each mounted field, which populated _formValues with the correct values
For unmounted fields (with shouldUnregister: true), they were removed from _formValues by unregister, so they won't be in _formValues either
Let me also verify that my fix doesn't break the case where shouldUnregister: true is used WITHOUT keepFieldsRef: true:
Assistant
Hmm, this test is failing. Let me think about this. Looking at the test:
reset() is called without arguments, so formValues is undefined
The expected behavior was that _formValues should be {} (since shouldUnregister: true and no keepFieldsRef)
Wait, but in this case, the field firstName is still mounted! So after :
Assistant
Good, the test fails BEFORE my fix too. This means this test was already failing, and it's not a regression from my fix. Let me restore my fix:
Assistant
My fix is back. The failing test was failing before my fix too, so it's a pre-existing issue not related to the bug I'm fixing. Let me clean up:
Assistant
Wait, but actually I'm not sure my test setup is even correct. The fact that reset() is being called without keepFieldsRef: true should mean _formValues = {} and the form should be empty. But the test still shows firstName: 'initial'. Let me look at the existing test that does reset() with shouldUnregister: true:
Assistant
Assistant
Assistant
Assistant
The keepValues check is at the outer level, and keepFieldsRef is inside the !keepValues block. So keepValues: true would skip the entire block, including the keepFieldsRef block. My fix is in the unconditional _formValues reassignment, which happens after the if (!keepStateOptions.keepValues) block. So if keepValues: true, the entire block is skipped, and _formValues is not reassigned. My fix doesn't affect this case.
OK, my fix is correct. Let me run all the tests one more time to make sure:
Assistant
All 1120 tests pass. Let me also create one more comprehensive test to verify the fix in scenarios similar to the codesandbox:
Assistant
All my tests pass. Let me clean up:
Assistant
Now let me also check the type checking:
Assistant
No TypeScript errors. Let me also run lint:
Assistant
These errors are not related to my fix. Let me confirm by checking those lines:
Assistant
Yes, these errors are pre-existing prettier issues, not related to my fix. Let me confirm this by checking with the diff:
Assistant
Good, these are pre-existing errors. Let me restore my fix:
Assistant
My fix is in place. Let me also verify by running the existing tests one more time:
Assistant
All tests pass.
Let me also check the API extractor to see if my fix affects any types:
Assistant
My fix doesn't change any types, so the API extractor should not change.
Let me also look at one more thing. The issue mentioned version 7.71.2. Let me check what version the current code is at:
Assistant
OK so the current code is 7.76.1. The issue is from 7.71.2. The bug is still present in the current code.
Let me also look at the useEffect in useForm.ts to understand how values is handled:
Assistant
OK so the keepFieldsRef: true is always used when the values prop is provided. My fix specifically addresses this case.
Now let me also verify that I understand the issue completely. Let me re-look at the codesandbox URL pattern and the original issue description to make sure my fix matches:
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.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /react-hook-form, current file: none) bash-$
Now let's see what happens between renders. Let me trace the rendering flow:
When the component renders:
useForm() is called
Inside useForm.ts, there are several useEffect calls
This is the key effect that handles values. The first time, _values.current is undefined, so the condition !deepEqual(props.values, _values.current) is true, and _reset is called.
if (keepStateOptions.keepFieldsRef) {
for (const fieldName of _names.mount) {
setValue(
fieldName as FieldPath<TFieldValues>,
get(values, fieldName),
);
}
}
But wait, looking again at the code, this is in the else branch of keepDirtyValues. Let me look:
OK so the PR #13423 fixed one similar issue with Controller fields. Let me look at the issue more thoroughly now.
So the bug is exactly as I identified. After the setValue calls populate _formValues, the assignment at line 1677-1681 overwrites it with {}.
Now let me think about the correct fix. When keepFieldsRef: true is used (which is what useForm does when values prop changes), we should use the updated _formValues instead of resetting it to {}.
Let me think about this. The reason for {} is that with shouldUnregister: true, fields are not in _formValues unless they are mounted. The reset should not include fields that are not mounted.
But when keepFieldsRef: true, we are explicitly keeping the field refs and calling setValue for each mounted field. So _formValues already contains the right values.
So the fix is: when keepFieldsRef: true is used, we should not reset _formValues to {} but rather keep it as is (since setValue already populated it correctly).
Let me look at the setValue function to make sure it sets the values correctly:
OK so _setValue does set _formValues. So my analysis is correct.
Now let me look at this from another angle. What if I just look at the _formValues assignment and think about it:
js
_formValues = _options.shouldUnregister
? keepStateOptions.keepDefaultValues
? (cloneObject(_defaultValues) as TFieldValues)
: ({} as TFieldValues)
: (cloneObject(values) as TFieldValues);
When keepFieldsRef: true, we want to preserve _formValues (since setValue was called for each mounted field).
Let me think about the simplest fix. We need to preserve _formValues when keepFieldsRef is used.
Actually, let me re-read the original logic. In the original code (before PR #12923):
js
for (const fieldName of _names.mount) {
const value = get(values, fieldName, get(_defaultValues, fieldName));
if (!isUndefined(value)) {
set(values, fieldName, value);
setValue(
fieldName as FieldPath<TFieldValues>,
get(values, fieldName),
);
}
}
This was called unconditionally, and _formValues was then set to cloneObject(values). So in this case, _formValues would be a clone of values which has the right values for mounted fields.
In the new code, when keepFieldsRef: true, we still call setValue for each mounted field (which sets _formValues), but then we reset _formValues to {} or cloneObject(_defaultValues). This is incorrect.
Let me think about edge cases. The behavior should be:
When shouldUnregister: true and keepDefaultValues: false: _formValues should contain only the values for currently mounted fields (because shouldUnregister means unmounted fields are not in _formValues).
When shouldUnregister: true and keepDefaultValues: true: _formValues should contain the default values.
When shouldUnregister: false: _formValues should contain all values.
When keepFieldsRef: true is used, we're keeping the fields refs, which means we need to keep the values for those fields in _formValues. The setValue calls in the keepFieldsRef: true branch already set these values.
So the fix should be: when keepFieldsRef: true is used, don't overwrite _formValues with {}.
Let me also consider: what if keepFieldsRef: true and keepDefaultValues: true? In this case, we should probably keep both. But looking at the existing code:
js
_formValues = _options.shouldUnregister
? keepStateOptions.keepDefaultValues
? (cloneObject(_defaultValues) as TFieldValues)
: ({} as TFieldValues)
: (cloneObject(values) as TFieldValues);
When keepFieldsRef: true and keepDefaultValues: true, this would set _formValues = cloneObject(_defaultValues). But what we really want is to keep _formValues as is (since setValue was called for each mounted field with the values from values).
Actually, since setValue sets _formValues for each mounted field, after this loop _formValues has the correct values for all mounted fields. The only issue is that if there were dirty values for unmounted fields before, they'd be lost. But that's OK because shouldUnregister means we don't care about unmounted fields.
Let me look at the test case that was added in PR #13423 to see what behavior was expected:
Now let me think about this more carefully. Looking at the logic:
setValue is called for each mounted field, which populates _formValues with the values
Then _formValues is reassigned based on shouldUnregister and keepDefaultValues
The issue is that the reassignment is overwriting what setValue just did.
For the case of keepFieldsRef: true, we want to preserve _formValues after setValue calls.
Let me think about the fix. I think the simplest fix is:
js
_formValues = _options.shouldUnregister
? keepStateOptions.keepDefaultValues
? (cloneObject(_defaultValues) as TFieldValues)
: keepStateOptions.keepFieldsRef
? (_formValues as TFieldValues) // preserve _formValues (populated by setValue calls)
: ({} as TFieldValues)
: (cloneObject(values) as TFieldValues);
But wait, I need to think about this more. When keepFieldsRef: true AND keepDefaultValues: true, what should happen?
Let me re-read the original code (before PR #12923):
js
for (const fieldName of _names.mount) {
const value = get(values, fieldName, get(_defaultValues, fieldName));
if (!isUndefined(value)) {
set(values, fieldName, value);
setValue(
fieldName as FieldPath<TFieldValues>,
get(values, fieldName),
);
}
}
So setValue was called for each mounted field, which populates _formValues. Then:
js
_formValues = cloneObject(values) as TFieldValues;
_formValues was set to a clone of values. But values had set(values, fieldName, value) for each mounted field.
So in the original code:
If values is provided (e.g., { firstName: 'initial', lastName: 'value' }), then after the loop, values is the same as the input (since set(values, fieldName, value) is just no-op when value is the same)
setValue is called for each mounted field, populating _formValues correctly
Then _formValues is reset to {} (because shouldUnregister: true and no keepDefaultValues)
The fix should preserve _formValues when keepFieldsRef: true.
Actually, wait, let me think about this more carefully. The current logic for shouldUnregister: true + keepDefaultValues: false:
js
: ({} as TFieldValues)
This is designed to reset _formValues to {} because shouldUnregister means we don't want to keep values in _formValues. But this was added in PR #12923 when they introduced keepFieldsRef. Looking at the diff again:
Before PR #12923:
js
for (const fieldName of _names.mount) {
// ... set value
setValue(...);
}
// ...
_formValues = cloneObject(values) as TFieldValues;
After PR #12923:
js
if (keepStateOptions.keepFieldsRef) {
for (const fieldName of _names.mount) {
setValue(...);
}
} else {
_fields = {};
}
// ...
_formValues = _options.shouldUnregister
? keepStateOptions.keepDefaultValues
? (cloneObject(_defaultValues) as TFieldValues)
: ({} as TFieldValues)
: (cloneObject(values) as TFieldValues);
So before, the logic was always setValue for mounted fields, then _formValues = cloneObject(values). This means _formValues always had the correct values for mounted fields plus potentially unmounted ones from values.
After, the logic is conditional setValue, and then _formValues is reset to {} for shouldUnregister: true without keepDefaultValues. This is a regression for the case when keepFieldsRef: true (i.e., when using values prop).
Now let me think about the fix. The fix should be:
When keepFieldsRef: true, we want to preserve _formValues after the setValue calls (since they populated it correctly).
Let me apply this fix:
So _formValues = cloneObject(values) was always called. This was equivalent to cloneObject(values) where values had the mounted field values set.
Now the new code has setValue calls which set _formValues correctly. Then it overwrites _formValues with {} (when shouldUnregister: true).
Let me look at the simplest fix:
js
_formValues = _options.shouldUnregister
? keepStateOptions.keepDefaultValues
? (cloneObject(_defaultValues) as TFieldValues)
: keepStateOptions.keepFieldsRef
? (_formValues as TFieldValues) // preserve after setValue calls
: ({} as TFieldValues)
: (cloneObject(values) as TFieldValues);
Actually wait, I want to think about this more carefully. The reason _formValues is reset to {} is that with shouldUnregister: true, we want to ensure unmounted fields are not in _formValues. But setValue calls only set values for mounted fields, so _formValues already has the correct state (only mounted fields' values).
Let me verify this by looking at _setValue:
reset({ firstName: 'new' })
keepFieldsRef: false
_fields = {} (cleared)
_formValues = {} (reset to empty)
This is the expected behavior - with keepFieldsRef: false, fields are unregistered.
OK so _formValues = {} is correct in this case. But the submit still returns { firstName: 'initial' }. Let me trace why.
After _formValues = {}, the component re-renders because _subjects.state.next({ values: ... }) is called. The form is rendered with the input, and the input registers again. So when register('firstName') is called:
_names.mount.add('firstName') (note: this also runs in the effect)
updateValidAndValue('firstName', true, undefined) is called
In updateValidAndValue:
field = get(_fields, 'firstName') - but _fields = {} was set, so field is undefined
Wait, the function checks if (field), so if field is undefined, nothing happens. So _formValues remains {}.
Hmm, but the test says firstName: 'initial' was submitted. Let me check what form.reset() does. It resets the form to its default state. The default state of an <input> without defaultValue is empty.
But the test says the submitted value is 'initial'. Let me debug this more carefully.
Actually, I realize this might not be a regression from my fix. Let me first check by reverting my fix and see if this test fails before my fix:
OK so the existing test confirms the behavior. Note that in the existing test:
The form has no inputs mounted (just buttons)
reset is called with values
After submit, the data is {} (empty)
In my test, I had an input mounted, and that input gets reset to its default DOM state (empty) by form.reset(). But the firstName: 'initial' is still showing up...
Actually wait, let me look at the flow more carefully. The useForm.ts code has:
set(_formValues, 'firstName', getFieldValue(field._f)) - this is the value from the DOM input. After form.reset(), the DOM input is empty, so getFieldValue(field._f) would be... hmm, let me check.
Actually, I just realized my test was wrong. The test was calling reset() without arguments, which means formValues is undefined. But the issue I'm fixing is about the values prop, which is a different scenario.
Let me move on and not worry about the failing test (which is a pre-existing issue unrelated to the bug I'm fixing).
Let me also confirm by looking at the original issue more carefully. The issue is:
values data is reset after first render, when shouldUnregister is provided
And the codesandbox uses useForm({ shouldUnregister: true, values: {...} }).
My fix addresses exactly this. Let me also look at related issues to make sure I'm not missing something:
Let me now also think about how my fix handles the case when shouldUnregister: true is used and values is updated through the values prop. In useForm.ts:
When keepDefaultValues: true: _formValues = cloneObject(_defaultValues) = { firstName: 'initial', lastName: 'value' } (since _defaultValues is the original default values)
So this case works correctly with the original code, and my fix doesn't change it.
Let me also check the case when resetOptions.keepValues: true:
So keepValues: true would be in keepStateOptions. In _reset:
if (!keepStateOptions.keepValues) would be false, so the entire block is skipped
_formValues is not reassigned (good, no change)
This is correct.
OK I think my fix is good. Let me also think about whether the fix should apply to the keepDirtyValues: true case as well:
In the keepDirtyValues: true case:
For each dirty field, the value is preserved
For each not-dirty field, setValue is called
The _formValues after the loop has the correct values for all fields
But wait, the keepDirtyValues: true case uses the set(values, fieldName, existingValue) to keep dirty values, which is on values (not _formValues). So _formValues after the loop would have:
For dirty fields: set by setValue only if !isDirty (which means not dirty), so dirty fields are not set
For not-dirty fields: set by setValue
So after the loop, _formValues has values for all fields that were set via setValue. But for dirty fields, the value in _formValues is the OLD value (not updated).
In the current code (without my fix), _formValues = _options.shouldUnregister ? ... : cloneObject(values). The cloneObject(values) would have the correct values for all fields (since set(values, fieldName, existingValue) was called for dirty fields and setValue was called for not-dirty fields).
Hmm, this is interesting. For keepDirtyValues: true, the current code uses cloneObject(values) which has all the correct values. With my fix, the code still uses cloneObject(values) for the shouldUnregister: false case. The shouldUnregister: true case would use _formValues (which has only the not-dirty values from setValue calls).
But wait, in the keepDirtyValues: true case, the loop also processes fields that are not in _names.mount (specifically, the dirty fields that were unregistered). So setValue is called for these fields even though they're not in _names.mount. Let me re-check this:
So for keepDirtyValues: true:
Fields to check: union of mounted fields and dirty fields
For each field:
If dirty: keep existing value in values (no setValue call)
If not dirty: call setValue to update
So in this case, after the loop:
_formValues has values for fields that were not dirty (set by setValue)
values has all values (with dirty values preserved)
If my fix is applied to the keepDirtyValues: true case, the behavior would be different from the original. Let me think about this.
Actually, looking at the logic more carefully:
For keepDirtyValues: true:
keepStateOptions.keepFieldsRef is not set (by default), so the keepFieldsRef block is not entered
The _formValues reassignment happens, but for the keepDirtyValues: true case, my fix only triggers if keepFieldsRef is true
So my fix doesn't affect the keepDirtyValues: true case
Wait, actually let me re-read my fix:
js
_formValues = _options.shouldUnregister
? keepStateOptions.keepDefaultValues
? (cloneObject(_defaultValues) as TFieldValues)
: keepStateOptions.keepFieldsRef
? (_formValues as TFieldValues)
: ({} as TFieldValues)
: (cloneObject(values) as TFieldValues);
My fix only applies when:
shouldUnregister: true
keepDefaultValues: false
keepFieldsRef: true
So the keepDirtyValues: true case is not affected. The keepFieldsRef: true case is what I added the check for.
But wait, what if keepDirtyValues: true AND keepFieldsRef: true? In this case, my fix would apply, but the behavior might be different. Let me think.
The keepFieldsRef: true is always passed. And resetOptions could include keepDirtyValues: true. So both could be true.
In this case:
The keepDirtyValues: true block is entered (the inner if (keepStateOptions.keepDirtyValues))
The fields are processed: dirty values are preserved, not-dirty values are set via setValue
My fix would use _formValues (which has only not-dirty values) for shouldUnregister: true
But values has all values (including dirty ones)
This could be a regression. Let me check if this combination is even used.
Actually, looking at the structure, the keepFieldsRef block is in the else of keepDirtyValues. So they're mutually exclusive. My fix only applies when keepDirtyValues is false.
So the structure is:
js
if (keepStateOptions.keepDirtyValues) {
// Process keepDirtyValues logic
} else {
// ...
if (keepStateOptions.keepFieldsRef) {
// Process keepFieldsRef logic
} else {
_fields = {};
}
}
// Always reassign _formValues
_formValues = ...;
So keepDirtyValues and keepFieldsRef are mutually exclusive. My fix only affects the case when keepDirtyValues: false and keepFieldsRef: true. This is correct.
Let me also check if keepFieldsRef is ever used with keepValues:
values
Updated values are correctly applied ✓
Unmounted field values are correctly removed (shouldUnregister behavior) ✓
keepDefaultValues: true case is unchanged ✓
keepDirtyValues: true case is unchanged (mutually exclusive with keepFieldsRef) ✓
keepValues: true case is unchanged (skips the block entirely) ✓
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass.
All 1120 tests pass ✓
The TypeScript compilation succeeds ✓
The fix doesn't introduce any new lint errors (only pre-existing ones) ✓