Skip to content

Comments

fix(validation): batch isValidating state updates with validation result#13181

Merged
bluebill1049 merged 4 commits intoreact-hook-form:masterfrom
a28689604:fix/batch-isValidating-state-updates
Dec 7, 2025
Merged

fix(validation): batch isValidating state updates with validation result#13181
bluebill1049 merged 4 commits intoreact-hook-form:masterfrom
a28689604:fix/batch-isValidating-state-updates

Conversation

@a28689604
Copy link
Contributor

Proposed Changes

Issue #13156 reports an unexpected transition in formState.isValidating when using a resolver. During validation, isValidating briefly flips from true to false before the resolver has finished updating errors, which makes the form temporarily look valid even though validation hasn’t finished yet.

After investigating, the cause is that the current implementation updates isValidating inside an async function containing an await, which breaks React’s state batching. As a result, the isValidating: false update is emitted separately from the subsequent errors and other updates.

Relevant code:

const _runSchema = async (name?: InternalFieldName[]) => {
_updateIsValidating(name, true);
const result = await _options.resolver!(
_formValues as TFieldValues,
_options.context,
getResolverOptions(
name || _names.mount,
_fields,
_options.criteriaMode,
_options.shouldUseNativeValidation,
),
);
_updateIsValidating(name);
return result;
};

Because the second _updateIsValidating runs after an awaited promise, React flushes it independently, creating an intermediate inconsistent state:

  • isValidating === false
  • errors[field] === undefined

This is exactly what the reproduction shows.

Changes

To ensure isValidating: false is batched together with the resolver result and the following state updates, the call to _updateIsValidating(name) has been moved out of _runSchema and placed immediately after the await _runSchema(...) call.

This guarantees the state transition occurs in the same flush as the resolver output, preventing the temporary invalid intermediate state.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

fixes: #13156

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing tests pass locally with my changes

@a28689604 a28689604 force-pushed the fix/batch-isValidating-state-updates branch from d3554cd to 9188b98 Compare December 5, 2025 17:15
Previously, _runSchema called _updateIsValidating() internally after
awaiting the resolver, which prevented batching with subsequent state
updates. Moving this call to each caller enables better batching and
reduces unnecessary re-renders.

Signed-off-by: a28689604 <[email protected]>
@a28689604 a28689604 force-pushed the fix/batch-isValidating-state-updates branch from 9188b98 to 9bab13c Compare December 6, 2025 04:58
@bluebill1049 bluebill1049 merged commit 831c0f2 into react-hook-form:master Dec 7, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

issue: Inconsistent formState during resolver validation (isValidating=false but errors not populated yet)

2 participants