fix(useForm): recompute isValid after reset when values update asynchronously#13126
Merged
bluebill1049 merged 2 commits intoreact-hook-form:masterfrom Nov 9, 2025
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 <81345825+a28689604@users.noreply.github.com>
bluebill1049
reviewed
Nov 4, 2025
src/logic/createFormControl.ts
Outdated
| defaultValues: _defaultValues as FormState<TFieldValues>['defaultValues'], | ||
| }); | ||
|
|
||
| !keepStateOptions.keepIsValid && _setValid(); |
Member
There was a problem hiding this comment.
Can this logic move to the hook level?
Contributor
Author
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.
Contributor
Author
|
@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. |
Member
|
yea keep dirty shouldn't affect isValid flag |
bluebill1049
approved these changes
Nov 9, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: