useFieldArray doesn’t remove correct indexes

Describe the bug
I have discovered that after removing 5 indexes, wait for the render, then remove 5 other indexes, this time one of the index will be wrongly removed.

To Reproduce
On CSB: add all the possible attributes on the select field, then remove the two last ones. The first remove will be fine, on the second one, variants should be removed from index 9 to 17, but variant n°2 is removed, and variant n°9 is kept.
(Check console for id’s and indexes)

Codesandbox link (Required)
https://codesandbox.io/s/problem-remove-usefieldarray-m8zub?file=/src/App.js

Expected behavior
Elements at indexes should be removed correctly.

Workaround
Using remove in a forEach instead of passing an array of indexes does the work.

Desktop (please complete the following information):

  • OS: Ubuntu 20
  • Browser : Chrome
  • Version : 87.0.4280.66

Additional context
I’m making a form to add products variations, depending on attributes (Sizes, Colors, Cut, etc.), I discovered this bug when testing my migration from formik (it was triggering 30 renders per product variant update) to react-hook-form.

1 possible answer(s) on “useFieldArray doesn’t remove correct indexes