Wait, I need to think about this more carefully. Let me trace through the code again.
Looking at the _reset function in createFormControl.ts:
When shouldUnregister is true:
- Line 1683:
_formValues = _options.shouldUnregister ? keepStateOptions.keepDefaultValues ? (cloneObject(_defaultValues) as TFieldValues) : ({} as TFieldValues) : (cloneObject(values) as TFieldValues);
So when shouldUnregister is true and keepDefaultValues is false/undefined, _formValues is set to {}.
Now, in useForm.ts, lines 153-167:
React.useEffect(() => {
if (props.values && !deepEqual(props.values, _values.current)) {
control._reset(props.values, {
keepFieldsRef: true,
...control._options.resetOptions,
});
if (!control._options.resetOptions?.keepIsValid) {
control._setValid();
}
_values.current = props.values;
updateFormState((state) => ({ ...state }));
} else {
control._resetDefaultValues();
}
}, [control, props.values]);
Wait, I see it now. The condition is:
- If
props.values exists AND is different from _values.current, then reset with the new values
- Otherwise, call
_resetDefaultValues()
The else branch calls _resetDefaultValues() which is defined as:
const _resetDefaultValues = () =>
isFunction(_options.defaultValues) &&
(_options.defaultValues as Function)().then((values: TFieldValues) => {
reset(values, _options.resetOptions);
_subjects.state.next({
isLoading: false,
});
});
This only runs if defaultValues is a function, so it's essentially a no-op for most cases.
But wait, the issue is different. Let me look at what happens when the form is submitted. When a form is submitted and the component re-renders, the effect on lines 153-167 runs again. Since props.values hasn't changed, the condition props.values && !deepEqual(props.values, _values.current) would be false (because _values.current = props.values was set in the previous run). So the else branch runs and calls _resetDefaultValues().
But _resetDefaultValues() only does something if defaultValues is a function. So if defaultValues is not a function, this should be a no-op.
Hmm, let me look at the issue description again. The issue says "values data is reset after first render". This suggests that after the first render (or after submitting), the values are being reset.
Actually, I think I need to look at what happens during the initial mount. On the first render:
_values.current is undefined (from line 56)
- The effect runs with
props.values set to some value
- The condition
props.values && !deepEqual(props.values, _values.current) is true because _values.current is undefined
- So
control._reset(props.values, ...) is called
_values.current = props.values is set
Now, the _reset function is called. Let me look at what it does when shouldUnregister is true. Looking at line 1683 in createFormControl.ts, if shouldUnregister is true and keepDefaultValues is not true, then _formValues will be set to {}. But the function also updates the field values and calls _subjects.state.next({ values: { ...values } }) which should broadcast the correct values.
Wait, I think I see the issue now. The _formValues is set to {} when shouldUnregister is true, but then the form values are set correctly via setValue calls. However, there might be a race condition or timing issue where the form state is updated with the wrong values.
Actually, I think the issue is in the effect on lines 135-140:
React.useEffect(() => {
props.shouldUnregister &&
control._subjects.state.next({
values: control._getWatch(),
});
}, [control, props.shouldUnregister]);
This effect runs when shouldUnregister changes. If shouldUnregister is true, it broadcasts the current watched values. But control._getWatch() might return an empty object if the form values haven't been set yet!
Let me think about the order of effects:
useIsomorphicLayoutEffect runs first (lines 92-112)
React.useEffect for props.disabled runs (lines 114-117)
React.useEffect for props.mode and props.reValidateMode runs (lines 119-126)
React.useEffect for props.errors runs (lines 128-133)
React.useEffect for props.shouldUnregister runs (lines 135-140)
React.useEffect for control._proxyFormState.isDirty runs (lines 142-151)
React.useEffect for props.values runs (lines 153-167)
So the effect for props.shouldUnregister (lines 135-140) runs BEFORE the effect for props.values (lines 153-167). This means that when shouldUnregister is true, it broadcasts control._getWatch() which might return an empty object because the form hasn't been reset with props.values yet!
That's the bug! The effect for props.shouldUnregister runs before the effect for props.values, so it broadcasts empty values before the form has been populated with props.values.
Wait, but the effect for props.shouldUnregister is in a React.useEffect, and the effect for props.values is also in a React.useEffect. They should run in order during the commit phase. So the shouldUnregister effect would run first and broadcast empty values, then the values effect would run and reset the form with the correct values.
But the issue is that the shouldUnregister effect broadcasts values: control._getWatch() which updates the form state. Then the values effect resets the form and calls updateFormState((state) => ({ ...state })) which should update the form state again.
Hmm, but maybe the issue is that the shouldUnregister effect is causing the form values to be cleared, and then the values effect doesn't properly restore them?
Let me look at the _getWatch function to see what it returns.
I think the real issue is that the effect for props.shouldUnregister (lines 135-140) runs on every render when props.shouldUnregister is true. But props.shouldUnregister doesn't change, so this effect should only run once on mount. However, the dependency array is [control, props.shouldUnregister], and control might change? No, control is from _formControl.current.control which should be stable.
Actually, wait. Let me re-read the effect:
React.useEffect(() => {
props.shouldUnregister &&
control._subjects.state.next({
values: control._getWatch(),
});
}, [control, props.shouldUnregister]);
This effect only runs when control or props.shouldUnregister changes. Since control is stable (from a ref), this should only run once when props.shouldUnregister is first set to true.
So the issue must be something else. Let me think about what happens when the form is submitted.
When the form is submitted:
handleSubmit is called
- The form state is updated (isSubmitting, isSubmitted, etc.)
- The component re-renders
- The effects run again
During the re-render, the effect for props.values (lines 153-167) runs. The condition is props.values && !deepEqual(props.values, _values.current). Since _values.current was set to props.values in the previous run, this condition should be false (assuming props.values hasn't changed). So the else branch runs and calls control._resetDefaultValues().
But wait, _resetDefaultValues() only runs if defaultValues is a function. So if the user doesn't provide defaultValues as a function, this should be a no-op.
Let me look at the issue description again. The user says "Simply submit a form" and "See error". But they don't specify what the error is. Let me check the CodeSandbox link.
Actually, I don't have access to the CodeSandbox link. Let me look at the issue more carefully.
The issue says "values data is reset after first render". So after the first render (or after submitting), the values are reset.
Let me think about what could cause this. Maybe the issue is that when shouldUnregister is true, the _formValues is set to {} in the _reset function, and then the form state is updated with empty values?
Looking at the _reset function again, after setting _formValues, it calls:
_subjects.array.next({
values: { ...values },
});
_subjects.state.next({
values: { ...values } as TFieldValues,
});
So it broadcasts the correct values (the ones passed to _reset), not the _formValues (which might be {}).
But then, there's another state update at the end of _reset:
_subjects.state.next({
submitCount: ...,
isDirty: ...,
isSubmitted: ...,
dirtyFields: ...,
touchedFields: ...,
errors: ...,
isSubmitSuccessful: ...,
isSubmitting: false,
defaultValues: _defaultValues as FormState<TFieldValues>['defaultValues'],
});
This doesn't include values, so it shouldn't affect the form values.
Hmm, I'm not sure what the issue is. Let me look at the tests to see if there's a similar test case.
Actually, let me re-read the code in useForm.ts more carefully. Looking at lines 153-167:
React.useEffect(() => {
if (props.values && !deepEqual(props.values, _values.current)) {
control._reset(props.values, {
keepFieldsRef: true,
...control._options.resetOptions,
});
if (!control._options.resetOptions?.keepIsValid) {
control._setValid();
}
_values.current = props.values;
updateFormState((state) => ({ ...state }));
} else {
control._resetDefaultValues();
}
}, [control, props.values]);
Wait, I see it now! The else branch is executed when props.values is falsy OR when props.values is equal to _values.current. But the issue is that when the effect runs on the initial mount, _values.current is undefined, so the condition is true and _reset is called. Then _values.current is set to props.values.
On subsequent renders (e.g., after submit), if props.values is the same object (or deeply equal), the condition is false, and the else branch runs, calling control._resetDefaultValues().
But _resetDefaultValues() is supposed to be a no-op if defaultValues is not a function. So what's the issue?
OH WAIT! I think I see it now. Let me look at _resetDefaultValues again:
const _resetDefaultValues = () =>
isFunction(_options.defaultValues) &&
(_options.defaultValues as Function)().then((values: TFieldValues) => {
reset(values, _options.resetOptions);
_subjects.state.next({
isLoading: false,
});
});
This function calls isFunction(_options.defaultValues). If defaultValues is not a function, it returns undefined (due to the short-circuit evaluation). But what if _options.defaultValues is undefined? Then isFunction(undefined) returns false, and the function returns undefined.
So the else branch should be a no-op if defaultValues is not a function. But wait, what if the user provides values prop without defaultValues? Then _options.defaultValues is undefined, and _resetDefaultValues() is a no-op.
Hmm, let me look at the _reset function again. When _reset is called with props.values, it sets _defaultValues to updatedValues (line 1624):
if (!keepStateOptions.keepDefaultValues) {
_defaultValues = updatedValues;
}
So after the first _reset, _defaultValues is set to props.values. This means that on subsequent renders, _resetDefaultValues() would check if _options.defaultValues is a function. If the user doesn't provide defaultValues as a function, this is still a no-op.
I'm still not sure what the issue is. Let me look at the _reset function more carefully to see if there's something that causes the values to be reset.
Actually, wait. Let me look at line 1683 again:
_formValues = _options.shouldUnregister
? keepStateOptions.keepDefaultValues
? (cloneObject(_defaultValues) as TFieldValues)
: ({} as TFieldValues)
: (cloneObject(values) as TFieldValues);
When shouldUnregister is true and keepDefaultValues is false (which is the default), _formValues is set to {}. This is the form's internal values, which might be used by getValues() or other functions.
But then, the _reset function also calls setValue for each field (line 1669-1673):
if (keepStateOptions.keepFieldsRef) {
for (const fieldName of _names.mount) {
setValue(
fieldName as FieldPath<TFieldValues>,
get(values, fieldName),
);
}
}
So the field values are set correctly, but _formValues is set to {}.
Now, when getValues() is called, it might return _formValues or the actual field values. Let me look at the getValues function.
Actually, let me think about this differently. The issue says "values data is reset after first render". This suggests that after the first render (or after some re-render), the values are cleared.
I think the issue might be that when the effect for props.values runs on the initial mount, it calls _reset with props.values. But in _reset, when shouldUnregister is true, _formValues is set to {}. Then, the effect broadcasts the form state update, which might include _formValues as the values.
But wait, the _reset function calls _subjects.state.next({ values: { ...values } }) (line 1690-1692), which broadcasts the correct values, not _formValues.
Hmm, I'm stuck. Let me look at the issue from a different angle. Maybe the issue is that the else branch is incorrect and shouldn't call _resetDefaultValues().
Actually, I think I found the issue! Looking at the effect again:
React.useEffect(() => {
if (props.values && !deepEqual(props.values, _values.current)) {
control._reset(props.values, {
keepFieldsRef: true,
...control._options.resetOptions,
});
if (!control._options.resetOptions?.keepIsValid) {
control._setValid();
}
_values.current = props.values;
updateFormState((state) => ({ ...state }));
} else {
control._resetDefaultValues();
}
}, [control, props.values]);
The issue is that the else branch is executed when props.values is falsy OR when props.values is equal to _values.current. But when props.values is equal to _values.current, we shouldn't do anything! The else branch should only be executed when props.values is falsy (i.e., not provided).
Wait, but the else branch calls _resetDefaultValues(), which is a no-op if defaultValues is not a function. So even if the else branch is executed, it shouldn't cause any issues.
Unless... wait, let me look at _resetDefaultValues again. Oh! I see it now. _resetDefaultValues checks isFunction(_options.defaultValues). But _options is updated in useForm.ts (line 90):
control._options = props;
So _options.defaultValues is props.defaultValues. If the user doesn't provide defaultValues, this is undefined. But what if the user provides values without defaultValues? Then _options.defaultValues is undefined, and isFunction(undefined) returns false, so _resetDefaultValues() is a no-op.
But wait, what if the user provides defaultValues as an object (not a function)? Then isFunction(defaultValues) returns false, and _resetDefaultValues() is still a no-op.
Hmm, I'm still not sure. Let me look at the initialization of _defaultValues in createFormControl.ts:
let _defaultValues =
isObject(_options.defaultValues) || isObject(_options.values)
? cloneObject(_options.defaultValues || _options.values) || {}
: {};
So if the user provides values without defaultValues, _defaultValues is set to cloneObject(_options.values). This makes sense.
Now, in _reset, when _reset is called with props.values, it sets _defaultValues to updatedValues (if keepDefaultValues is false). So after the first _reset, _defaultValues is set to props.values.
But then, what happens on subsequent renders? The effect for props.values runs again, and the condition is props.values && !deepEqual(props.values, _values.current). Since _values.current is set to props.values in the previous run, this condition is false (assuming props.values is the same object or deeply equal). So the else branch runs, calling _resetDefaultValues().
But _resetDefaultValues() checks isFunction(_options.defaultValues), which is props.defaultValues. If the user doesn't provide defaultValues as a function, this is false, and _resetDefaultValues() is a no-op.
OK so I think the issue is NOT in the else branch. The issue must be something else.
Let me think about the order of operations again. On the initial mount:
useForm is called with props.values and props.shouldUnregister: true
_formControl.current is set (if not already set)
control._options = props is set
- The effects run in order:
a.
useIsomorphicLayoutEffect subscribes to form state changes
b. React.useEffect for props.disabled
c. React.useEffect for props.mode and props.reValidateMode
d. React.useEffect for props.errors
e. React.useEffect for props.shouldUnregister (if shouldUnregister is true, it broadcasts control._getWatch())
f. React.useEffect for control._proxyFormState.isDirty
g. React.useEffect for props.values (if props.values is provided, it calls _reset)
So on the initial mount, the effect for props.shouldUnregister runs before the effect for props.values. This means that when shouldUnregister is true, it broadcasts control._getWatch() which might return an empty object because the form hasn't been reset with props.values yet!
But wait, the effect for props.shouldUnregister only runs when control or props.shouldUnregister changes. On the initial mount, props.shouldUnregister is true, so the effect runs. But then, on subsequent renders, props.shouldUnregister is still true, so the effect doesn't run again (unless control changes, which it shouldn't).
So the effect for props.shouldUnregister only runs once on the initial mount. And at that point, the form hasn't been reset with props.values yet, so control._getWatch() might return an empty object.
But then, the effect for props.values runs and resets the form with props.values. This should update the form state with the correct values.
Hmm, but maybe the issue is that the form state is updated with empty values from the shouldUnregister effect, and then the values effect resets the form, but the form state is not properly updated?
Actually, wait. Let me look at the _getWatch function to see what it returns.
Let me search for _getWatch in createFormControl.ts.
OK I realize I've been going in circles. Let me take a step back and think about what the actual bug is.
The issue description says: "values data is reset after first render, when shouldUnregister is provided"
This suggests that:
- The form is rendered with
values and shouldUnregister: true
- After the first render (or after submit), the values are reset
The reproduction steps are:
- Simply submit a form
- See error
So the issue is that after submitting the form, the values are reset.
Now, let me think about what happens when the form is submitted. When the form is submitted:
handleSubmit is called
- The form state is updated (isSubmitting, isSubmitted, etc.)
- The component re-renders
- The effects run again
During the re-render, the effect for props.values (lines 153-167) runs. The condition is props.values && !deepEqual(props.values, _values.current). Since _values.current was set to props.values in the initial render, this condition should be false (assuming props.values is the same object). So the else branch runs and calls control._resetDefaultValues().
But wait! What if props.values is a new object on each render? For example, if the parent component passes { firstName: 'test' } as values, but this object is created on each render (not memoized), then deepEqual(props.values, _values.current) might still return true (because deepEqual checks the values, not the reference). So the condition should still be false.
Hmm, but what if deepEqual has a bug? Let me look at the deepEqual function.
Actually, I think I need to run the tests to see what the actual behavior is. Let me create a simple test and run it.
Actually, let me re-read the effect one more time:
React.useEffect(() => {
if (props.values && !deepEqual(props.values, _values.current)) {
control._reset(props.values, {
keepFieldsRef: true,
...control._options.resetOptions,
});
if (!control._options.resetOptions?.keepIsValid) {
control._setValid();
}
_values.current = props.values;
updateFormState((state) => ({ ...state }));
} else {
control._resetDefaultValues();
}
}, [control, props.values]);
Wait, I just realized something. The condition is:
if (props.values && !deepEqual(props.values, _values.current))
This means:
- If
props.values is truthy AND props.values is NOT deeply equal to _values.current, then reset with props.values.
- Otherwise (if
props.values is falsy OR if they ARE deeply equal), call _resetDefaultValues().
So on the initial render:
props.values is { firstName: 'test' }
_values.current is undefined
deepEqual({ firstName: 'test' }, undefined) returns false
!deepEqual({ firstName: 'test' }, undefined) returns true
- So the condition is true, and
_reset is called
On subsequent renders (e.g., after submit):
props.values is { firstName: 'test' } (same value)
_values.current is { firstName: 'test' } (set from previous render)
deepEqual({ firstName: 'test' }, { firstName: 'test' }) returns true
!deepEqual({ firstName: 'test' }, { firstName: 'test' }) returns false
- So the condition is false, and
_resetDefaultValues() is called
But _resetDefaultValues() is a no-op if defaultValues is not a function. So what's the issue?
OH WAIT! I think I see it now. Let me look at _resetDefaultValues again:
const _resetDefaultValues = () =>
isFunction(_options.defaultValues) &&
(_options.defaultValues as Function)().then((values: TFieldValues) => {
reset(values, _options.resetOptions);
_subjects.state.next({
isLoading: false,
});
});
This function calls isFunction(_options.defaultValues). If defaultValues is not a function, it returns false (due to short-circuit evaluation). But false is still a truthy value in the context of the else branch... wait, no. The function returns false or undefined, which are both falsy. So the function is a no-op if defaultValues is not a function.
Hmm, but wait. The function doesn't return anything if defaultValues is a function (it returns the result of .then() which is a Promise). But if defaultValues is not a function, it returns false.
Actually, I think the issue is that I'm overcomplicating this. Let me look at the actual behavior of the _reset function when shouldUnregister is true.
When _reset is called with props.values and shouldUnregister is true:
_formValues is set to {} (if keepDefaultValues is false)
- The field values are set via
setValue (if keepFieldsRef is true)
_subjects.state.next({ values: { ...values } }) is called, broadcasting the correct values
So the form values should be correct after the _reset call. But the issue is that _formValues is set to {}, which might cause issues later.
Let me look at what getValues returns. If getValues returns _formValues, then it would return {} instead of the correct values.
Let me search for getValues in createFormControl.ts.
Actually, let me just run the test and see what happens. But first, let me understand what the fix should be.
I think the issue is that when _reset is called with props.values and shouldUnregister is true, it sets _formValues to {}. But we want to keep the values, so we should pass keepDefaultValues: true to _reset.
Wait, but keepDefaultValues affects whether _defaultValues is updated, not whether _formValues is set to {}. Let me look at the _reset function again:
_formValues = _options.shouldUnregister
? keepStateOptions.keepDefaultValues
? (cloneObject(_defaultValues) as TFieldValues)
: ({} as TFieldValues)
: (cloneObject(values) as TFieldValues);
So when shouldUnregister is true:
- If
keepDefaultValues is true, _formValues is set to _defaultValues
- If
keepDefaultValues is false, _formValues is set to {}
But wait, on the initial call to _reset, _defaultValues is set to updatedValues (line 1624):
if (!keepStateOptions.keepDefaultValues) {
_defaultValues = updatedValues;
}
So if keepDefaultValues is false, _defaultValues is updated to updatedValues (which is cloneObject(props.values)). Then, _formValues is set to {} (because keepDefaultValues is false).
So the issue is that _formValues is set to {} instead of props.values. This might cause getValues() to return {} instead of the correct values.
But wait, the _reset function also calls setValue for each field (if keepFieldsRef is true):
if (keepStateOptions.keepFieldsRef) {
for (const fieldName of _names.mount) {
setValue(
fieldName as FieldPath<TFieldValues>,
get(values, fieldName),
);
}
}
So the field values are set correctly. But _formValues is still {}.
Now, let me look at what getValues returns. If getValues returns the field values (from _formValues), then it would return {}. But if getValues reads from the field references, it would return the correct values.
Let me search for getValues in createFormControl.ts.
Actually, I realize I need to just look at the code. Let me search for the getValues function.
OK I think I need to stop overthinking and just look at the actual bug. Let me trace through the code more carefully.
Actually, I just realized something. Looking at the effect for props.values:
React.useEffect(() => {
if (props.values && !deepEqual(props.values, _values.current)) {
control._reset(props.values, {
keepFieldsRef: true,
...control._options.resetOptions,
});
if (!control._options.resetOptions?.keepIsValid) {
control._setValid();
}
_values.current = props.values;
updateFormState((state) => ({ ...state }));
} else {
control._resetDefaultValues();
}
}, [control, props.values]);
The issue is that when the effect runs on subsequent renders (when props.values hasn't changed), it calls control._resetDefaultValues(). But _resetDefaultValues() is a no-op if defaultValues is not a function.
However, the dependency array is [control, props.values]. If props.values is a new object on each render (not memoized), then props.values would change on each render, and the effect would run again. But deepEqual should still return true, so the condition should be false, and _resetDefaultValues() should be called.
But wait, the dependency array uses props.values, not _values.current. So if props.values is a new object on each render (even if it has the same values), the effect would run because props.values has changed (reference-wise). But then, deepEqual(props.values, _values.current) would still return true, so the condition would be false, and _resetDefaultValues() would be called.
So the issue is that _resetDefaultValues() is called on every render when props.values hasn't changed (in terms of values, not reference). But _resetDefaultValues() is a no-op if defaultValues is not a function.
Hmm, so maybe the issue is that _resetDefaultValues() is NOT a no-op in some cases. Let me look at _resetDefaultValues again:
const _resetDefaultValues = () =>
isFunction(_options.defaultValues) &&
(_options.defaultValues as Function)().then((values: TFieldValues) => {
reset(values, _options.resetOptions);
_subjects.state.next({
isLoading: false,
});
});
This checks isFunction(_options.defaultValues). But _options is props (from control._options = props). So this checks if props.defaultValues is a function.
But what if the user provides values without defaultValues? Then props.defaultValues is undefined, and isFunction(undefined) returns false. So _resetDefaultValues() is a no-op.
So I still don't see the issue. Let me think about this differently.
Wait, I just had another idea. What if the issue is that the else branch should NOT call _resetDefaultValues()? The else branch is executed when props.values is falsy OR when props.values is equal to _values.current. In the latter case, we shouldn't do anything because the values are already correct. But in the former case (when props.values is falsy), we might want to reset to default values.
But the current code calls _resetDefaultValues() in both cases, which is incorrect. When props.values is equal to _values.current, we shouldn't do anything. And when props.values is falsy, we might want to reset to default values (but only if defaultValues is a function).
So the fix should be to remove the else branch, or to add a condition to check if props.values is falsy before calling _resetDefaultValues().
Actually, looking at the code, I think the else branch is meant to handle the case when props.values is not provided (falsy). In this case, we want to reset to default values if defaultValues is a function (async default values). But the current code also calls _resetDefaultValues() when props.values is equal to _values.current, which is incorrect.
So the fix should be to change the condition:
if (props.values && !deepEqual(props.values, _values.current)) {
control._reset(props.values, {
keepFieldsRef: true,
...control._options.resetOptions,
});
if (!control._options.resetOptions?.keepIsValid) {
control._setValid();
}
_values.current = props.values;
updateFormState((state) => ({ ...state }));
} else if (!props.values) {
control._resetDefaultValues();
}
Or more simply:
if (props.values && !deepEqual(props.values, _values.current)) {
control._reset(props.values, {
keepFieldsRef: true,
...control._options.resetOptions,
});
if (!control._options.resetOptions?.keepIsValid) {
control._setValid();
}
_values.current = props.values;
updateFormState((state) => ({ ...state }));
} else if (!props.values) {
control._resetDefaultValues();
}
Wait, but this still doesn't explain the bug. The bug is that "values data is reset after first render, when shouldUnregister is provided". So the issue is that the values are reset, not that _resetDefaultValues() is called.
Let me think about this differently. Maybe the issue is that when _reset is called with shouldUnregister: true, it resets the form values to {}, and then the form is cleared.
Actually, wait. Let me re-read the _reset function. When _reset is called:
updatedValues = formValues ? cloneObject(formValues) : _defaultValues (line 1619)
cloneUpdatedValues = cloneObject(updatedValues) (line 1620)
isEmptyResetValues = isEmptyObject(formValues) (line 1621)
values = isEmptyResetValues ? _defaultValues : cloneUpdatedValues (line 1622)
- If
!keepStateOptions.keepDefaultValues, _defaultValues = updatedValues (line 1624-1626)
- If
!keepStateOptions.keepValues, the field values are updated (lines 1628-1675)
_formValues is set based on shouldUnregister (line 1683-1687)
_subjects.array.next({ values: { ...values } }) is called (lines 1689-1691)
_subjects.state.next({ values: { ...values } as TFieldValues }) is called (lines 1693-1695)
So after _reset, _formValues might be {}, but _subjects.state.next({ values: { ...values } }) broadcasts the correct values. So the form state should have the correct values.
But then, what if the form state is updated with _formValues somewhere else? Let me search for where _formValues is used.
Actually, I think the issue might be in the _getWatch function. Let me search for _getWatch in createFormControl.ts.
Let me actually look for _getWatch definition:
Actually, you know what, let me just look at what happens when the effect for props.shouldUnregister runs. It calls:
props.shouldUnregister &&
control._subjects.state.next({
values: control._getWatch(),
});
So it broadcasts values: control._getWatch(). Let me look at what _getWatch returns.
Let me search for _getWatch:
I realize I should search for the function definition directly.
<tool_call>
<tool_call>
<function=bash_terminal>
<parameter=command>
grep -n "_getWatch" /react-hook-form/src/logic/createFormControl.ts | head -20
</tool_call>