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.
Hi, I realize that the problem in my code was that it mandatory to use item.id in the main wrapper of fields. If you set another key it doesn’t work. Maybe you can throw an error in that case? Without putting the key in that way it was deleting the last one!
https://codesandbox.io/s/react-hook-form-usefieldarray-forked-6urgq