fix: ensure isValid updates after trigger() when accessed later#13213
Closed
jtomaszewski wants to merge 1 commit intoreact-hook-form:masterfrom
Closed
fix: ensure isValid updates after trigger() when accessed later#13213jtomaszewski wants to merge 1 commit intoreact-hook-form:masterfrom
jtomaszewski wants to merge 1 commit intoreact-hook-form:masterfrom
Conversation
b2a1bce to
7c130c9
Compare
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>
7c130c9 to
17431be
Compare
Contributor
Author
|
Raised this too early, will get back if I get a good repro (and maybe a fix). |
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.
Summary
Fixes an issue where
formState.isValiddoesn't update correctly after callingsetValue()with{ shouldValidate: true }or aftertrigger(), even when:trigger()returnstrueformState.errorsis empty{}Root Cause
The proxy in
getProxyFormStatereads from React state, but whenisValidisn't subscribed (accessed) beforetrigger()is called, the React state isn't updated. This causes stale values whenisValidis accessed later.Fix
createFormControl.tstrigger(): Store the computedisValidvalue in_formState.isValidafter the notification is emittedgetProxyFormState.ts: For theisValidkey, return the value fromcontrol._formStatedirectly, which always has the most up-to-date valueThe
_formState.isValidupdate is placed AFTER_subjects.state.next()to preserve the existing notification condition that comparesisValidwith_formState.isValid.Test Plan
isValidaftertrigger()without prior subscription)setValue+triggerwith built-in validationuseFieldArrayscenariosRelated Issues
This is the same issue reported in:
revalidateMode: "onSubmit"does not work correctly withuseFieldArray#12631🤖 Generated with Claude Code