Skip to content

🐞 fix(useFieldArray): ignore duplicated indexes and keep the indexes passed to remove unchanged - #13830

Merged
bluebill1049 merged 1 commit into
react-hook-form:masterfrom
charan-rathore:fix/remove-duplicate-and-mutated-indexes
Oct 5, 2026
Merged

bluebill1049 merged 1 commit into
react-hook-form:masterfrom
charan-rathore:fix/remove-duplicate-and-mutated-indexes

Conversation

@charan-rathore

Copy link
Copy Markdown
Contributor

Pull Request Template

Proposed Changes

remove from useFieldArray has two problems with the indexes you pass in:

  1. Duplicated indexes remove extra rows. For example with rows a, b, c, d, remove([1, 1]) removes both b and c, because the second 1 is applied after the array has already shifted. With [a, c], remove([0, 0]) removes everything.
  2. The array you pass in is sorted in place. remove([3, 1]) leaves your own array as [1, 3].

The fix removes duplicates and sorts a copy before the rows are removed. Output for normal calls is the same.

I added tests for the util and for useFieldArray.remove. They fail without the fix.

Fixes # (no issue, found while testing field array helpers)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing tests pass locally with my changes

…passed to remove unchanged

Signed-off-by: Charan Rathore <[email protected]>

@bluebill1049 bluebill1049 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bluebill1049
bluebill1049 merged commit c7cb33a into react-hook-form:master Oct 5, 2026
6 of 10 checks passed
@charan-rathore
charan-rathore deleted the fix/remove-duplicate-and-mutated-indexes branch October 5, 2026 22:48
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.

2 participants