Skip to content

fix: ensure isValid updates after trigger() when accessed later#13213

Closed
jtomaszewski wants to merge 1 commit intoreact-hook-form:masterfrom
fullstackhouse:vk/f0eb-fix-formstate-is
Closed

fix: ensure isValid updates after trigger() when accessed later#13213
jtomaszewski wants to merge 1 commit intoreact-hook-form:masterfrom
fullstackhouse:vk/f0eb-fix-formstate-is

Conversation

@jtomaszewski
Copy link
Copy Markdown
Contributor

Summary

Fixes an issue where formState.isValid doesn't update correctly after calling setValue() with { shouldValidate: true } or after trigger(), even when:

  • trigger() returns true
  • formState.errors is empty {}
  • The form values are valid according to the schema

Root Cause

The proxy in getProxyFormState reads from React state, but when isValid isn't subscribed (accessed) before trigger() is called, the React state isn't updated. This causes stale values when isValid is accessed later.

Fix

  1. In createFormControl.ts trigger(): Store the computed isValid value in _formState.isValid after the notification is emitted
  2. In getProxyFormState.ts: For the isValid key, return the value from control._formState directly, which always has the most up-to-date value

The _formState.isValid update is placed AFTER _subjects.state.next() to preserve the existing notification condition that compares isValid with _formState.isValid.

Test Plan

  • Added test case that reproduces the bug scenario (accessing isValid after trigger() without prior subscription)
  • Added tests for setValue + trigger with built-in validation
  • Added tests for useFieldArray scenarios
  • All existing tests pass (1037 tests)

Related Issues

This is the same issue reported in:

🤖 Generated with Claude Code

@jtomaszewski jtomaszewski force-pushed the vk/f0eb-fix-formstate-is branch from b2a1bce to 7c130c9 Compare December 22, 2025 10:46
Tests reproduce a bug where formState.isValid doesn't update correctly
after calling trigger() when isValid wasn't accessed before the trigger.

The bug occurs because:
1. The proxy in getProxyFormState reads from React state
2. When isValid isn't subscribed before trigger(), React state isn't updated
3. When isValid is accessed later, it returns stale value

Test cases added:
- resolver.test.tsx: renderHook test that accesses isValid AFTER trigger
- trigger.test.tsx: various setValue + trigger scenarios with built-in
  validation and useFieldArray

Related issues:
- react-hook-form#12684
- react-hook-form#12631

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@jtomaszewski jtomaszewski force-pushed the vk/f0eb-fix-formstate-is branch from 7c130c9 to 17431be Compare December 22, 2025 10:48
@jtomaszewski
Copy link
Copy Markdown
Contributor Author

Raised this too early, will get back if I get a good repro (and maybe a fix).

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.

1 participant