fix(useForm): recompute isValid after reset when values update asynchronously#13126
Conversation
…ronously - Call _setValid() at the end of _reset() unless keepIsValid is set. - Ensures formState.isValid updates correctly when props.values change asynchronously. Signed-off-by: Eric Chen <[email protected]>
src/logic/createFormControl.ts
Outdated
| defaultValues: _defaultValues as FormState<TFieldValues>['defaultValues'], | ||
| }); | ||
|
|
||
| !keepStateOptions.keepIsValid && _setValid(); |
There was a problem hiding this comment.
Can this logic move to the hook level?
There was a problem hiding this comment.
Sure, I’ve moved the code to useForm, and it now runs after the reset is completed.
- Call _setValid() in useForm when resetOptions.keepIsValid is false.
|
@bluebill1049 Please check this example: when clicking the button, the form resets with invalid data, but isValid should remain true; instead, it changes. Could you confirm if this is unintended behavior? If so, I’ll work on a fix and add a test file to cover this case. |
|
yea keep dirty shouldn't affect isValid flag |
Proposed Changes
Problem:
When props.values are set asynchronously, formState.isValid wasn’t recalculated because reset() sets control._state.mount = true, so the useForm mount effect that calls _setValid() no longer runs.
Root cause flow:
react-hook-form/src/useForm.ts
Lines 154 to 165 in 4922698
react-hook-form/src/useForm.ts
Lines 167 to 171 in 4922698
Fix:
Recompute validity at the end of _reset() while respecting keep-state options.
Fixes #13123
Type of change
Checklist: