fix: unregister previous field when switching conditional Controllers#13041
Merged
bluebill1049 merged 4 commits intoreact-hook-form:masterfrom Aug 30, 2025
Merged
Conversation
1 task
bluebill1049
approved these changes
Aug 30, 2025
Member
|
Thanks for the contribution and pr. |
1 task
shahmir-oscilar
added a commit
to shahmir-oscilar/react-hook-form
that referenced
this pull request
Mar 19, 2026
… first `getNodeParentName` finds the first `.\d+` segment in a field path. For nested field arrays like `steps.0.items.2.name`, it returns `steps` instead of `steps.0.items`, causing `isNameInFieldArray` to return false. This means the `!isArrayField` guard in useController's name-change cleanup (PR react-hook-form#13041) fails to skip the unregister, wiping form values for shifted items when the field array is nested under a numeric path. Fix: iterate over all `.\d+` segments and check each prefix against the field array name set. Fixes react-hook-form#13317 Co-authored-by: Claude Code <[email protected]>
bluebill1049
added a commit
that referenced
this pull request
Mar 22, 2026
…field arrays (#13318) * fix: isNameInFieldArray should check all ancestor paths, not just the first `getNodeParentName` finds the first `.\d+` segment in a field path. For nested field arrays like `steps.0.items.2.name`, it returns `steps` instead of `steps.0.items`, causing `isNameInFieldArray` to return false. This means the `!isArrayField` guard in useController's name-change cleanup (PR #13041) fails to skip the unregister, wiping form values for shifted items when the field array is nested under a numeric path. Fix: iterate over all `.\d+` segments and check each prefix against the field array name set. Fixes #13317 Co-authored-by: Claude Code <[email protected]> * refactor: extract shared FIELD_ARRAY_INDEX_PATTERN from getNodeParentName Co-authored-by: Claude Code <[email protected]> * Apply code review suggestions --------- Co-authored-by: Claude Code <[email protected]> Co-authored-by: Bill <[email protected]>
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.
fix #13039
This PR addresses the issue where field values could leak between Controllers rendered conditionally at the same position.
useControllernow tracks the previous field name and automatically unregisters it when the name changes, preventing removed fields from lingering in the form state. Field Array functionality is preserved.If users prefer handling this with explicit
keyprops, this PR can be closed! 🙂