fix: prevent field array ghost elements with keepDirtyValues#13188
Merged
bluebill1049 merged 3 commits intoreact-hook-form:masterfrom Jan 1, 2026
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where removed field array items were being re-inserted as "ghost" elements during form reset with keepDirtyValues enabled. The issue occurred because stale field names remained in _names.mount after removal, and the reset logic attempted to set values for non-existent array indices, inadvertently creating new array elements.
Key changes:
- Modified the
keepDirtyValueslogic to check if field values exist before setting them - Added comprehensive test coverage for the field array ghost elements scenario
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/logic/createFormControl.ts | Added undefined checks to prevent setting values for non-existent field paths, fixing the ghost elements bug |
| src/tests/useFieldArray/remove.test.tsx | Added comprehensive test that reproduces and validates the fix for the ghost elements issue |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/logic/createFormControl.ts
Outdated
Comment on lines
1368
to
1370
| if (isDirty && existingValue !== undefined) { | ||
| set(values, fieldName, existingValue); | ||
| } else if (!isDirty && newValue !== undefined) { |
Member
There was a problem hiding this comment.
can use the isUndefined util function
b08e960 to
71266aa
Compare
bluebill1049
approved these changes
Dec 20, 2025
Add existence checks in keepDirtyValues logic to skip fields that have been removed from the form values, preventing ghost array elements. Signed-off-by: a28689604 <a28689604@gmail.com>
Refactor the logic in createFormControl to use isUndefined checks instead of direct comparisons to undefined. Signed-off-by: a28689604 <a28689604@gmail.com>
e07bd74 to
20dc074
Compare
bluebill1049
approved these changes
Jan 1, 2026
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.
Proposed Changes
As mentioned in issue #12489, when using
useFieldArraywith thevaluesprop andresetOptions: { keepDirtyValues: true }, removed field array items were being re-inserted as "ghost" elements on subsequent appends.Root cause
During reset with
keepDirtyValues: true, the logic iterates through_names.mount(which contains field names liketest.0.value,test.1.value) to determine which values to preserve. However:_names.mountdirtyFieldsstate is also preserved from before the reset_formValuessetValue()with a path to a non-existent array index created ghost elements (e.g.,test.1.valuecreated[{ value: undefined }])Fixes
existingValue !== undefined)newValue !== undefined)This prevents the code from creating array elements for field paths that no longer exist.
Type of change
Fixes #12489
Checklist: