Skip to content

fix: unregister previous field when switching conditional Controllers#13041

Merged
bluebill1049 merged 4 commits intoreact-hook-form:masterfrom
m2na7:fix/prevent-field-value-leakage
Aug 30, 2025
Merged

fix: unregister previous field when switching conditional Controllers#13041
bluebill1049 merged 4 commits intoreact-hook-form:masterfrom
m2na7:fix/prevent-field-value-leakage

Conversation

@m2na7
Copy link
Copy Markdown
Contributor

@m2na7 m2na7 commented Aug 29, 2025

fix #13039

This PR addresses the issue where field values could leak between Controllers rendered conditionally at the same position.

useController now 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 key props, this PR can be closed! 🙂

@bluebill1049 bluebill1049 merged commit c33b4c7 into react-hook-form:master Aug 30, 2025
6 checks passed
@bluebill1049
Copy link
Copy Markdown
Member

Thanks for the contribution and pr.

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]>
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: Field values leak across conditional renders when Controllers share the same render position

2 participants