useFieldArray resets form values after deleting item
See original GitHub issueDescribe the bug
All items appended to a field array are reset to the value given at append
time when you delete an item from the stack.
To Reproduce Steps to reproduce the behavior:
- Go to https://codesandbox.io/s/react-hook-form-usefieldarray-vy8fv
- Click on the append button a few times
- Change the text value of the second item
- Delete the last item
Codesandbox link https://codesandbox.io/s/react-hook-form-usefieldarray-vy8fv
Expected behavior I would expect the value I entered into the textbox to remain after deleting another item from the array.
Screenshots If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
- OS: Mac 10.14.6
- Browser: Chrome
- Version 79.0.3945.13
Additional context
Interestingly enough, if you change the li
key to test[${index}]
, it will retain the values when you delete an item from the stack. Unfortunately, this appears to have a similar issue. If you click append a couple of times, change the values to a, b, and c and then delete b, it will delete c instead.
Thank you for this wonderful little library! 🍻
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:35 (20 by maintainers)
Using
ref={register()}
instead ofref={register}
works for me.had the same problem; fixed it by passing defaults AND
ref={register()}
(it didn’t work correctly withregister
acquired in the component viauseFormContext
.