question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useFieldArray resets form values after deleting item

See original GitHub issue

Describe 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:

  1. Go to https://codesandbox.io/s/react-hook-form-usefieldarray-vy8fv
  2. Click on the append button a few times
  3. Change the text value of the second item
  4. 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:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:35 (20 by maintainers)

github_iconTop GitHub Comments

6reactions
NoobTWcommented, Sep 12, 2020

had the same problem; fixed it by passing defaults AND ref={register()} (it didn’t work correctly with register acquired in the component via useFormContext.

Using ref={register()} instead of ref={register} works for me.

3reactions
miletichcommented, Jul 1, 2020

had the same problem; fixed it by passing defaults AND ref={register()} (it didn’t work correctly with register acquired in the component via useFormContext.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UsefieldArray react hook form deleting the last element only
Basically I'm trying to append and delete but the problem is when I'm deleting its deleting only the last element. append is working...
Read more >
React Hook Form Reset useFieldArray - CodeSandbox
A custom hook for working with Field Arrays (dynamic inputs). 0. 1.5 ...
Read more >
useForm - unregister - React Hook Form
This method allows you to unregister a single input or an array of inputs. ... touchedFields will no longer remove that input after...
Read more >
React Hook Form 7 - Dynamic Form Example with useFieldArray
The "Buy Tickets" button simply displays the form values in an alert popup if the form is valid, and the "Reset" button resets...
Read more >
Effective forms: building dynamic array fields with useFieldArray
replace – replaces the total field array values; remove – removes input/inputs at a particular position – or removes all when no index...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found