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 with nested arrays throws error with `append` and `remove` in nested array (data is not iterable)

See original GitHub issue

Describe the bug

Creating a nested useFieldArray

setValue("test", [
  ...(getValues().test || []),
  {
    name: "append",
    nestedArray: [{ field1: "append", field2: "append" }]
  }
]);

does not populate the fields for nestedArray values field1 and field2

Using

remove(k)

to remove the empty nestedArray throws an error. removeAtIndexes - data is not iterable


Creating a nested useFieldArray

setValue("test", [
  ...(getValues().test || []),
  {
    name: "append",
    nestedArray: [{ field1: "append", field2: "append" }]
  }
]);

and then using

append({
  field1: "field1",
  field2: "field2"
})

to append a nested array throws an error append - data is not iterable


Using

append({ name: "append" });

to create a single level array works.

Using

append({
  field1: "field1",
  field2: "field2"
})

to populate the nestedArray works

Using

setValue("test", [
  ...(getValues().test || []),
  {
    name: "append",
    nestedArray: [{ field1: "append", field2: "append" }]
  }
]);

to create a new nested array clears the values of the other nestedArray

This seems like it could be somewhat related to #5208


To Reproduce Steps to reproduce the behavior:

  1. Go to https://codesandbox.io/s/react-hook-form-usefieldarray-nested-arrays-forked-cg4yj?file=/src/index.js
  2. Click on ‘Append Nested’ button
  3. Notice the fields are not populated with default values
  4. Click on ‘Delete Nested’ button
  5. Observe thrown error
  6. Clear the error and refresh the page
  7. Click on ‘Append Nested’ button
  8. Click on the new ‘Append Nested’ button that appears above the hr tag
  9. Observe error
  10. Clear the error and refresh the page
  11. Click the ‘Append’ button
  12. Click on the new ‘Append Nested’ button that appears above the hr tag
  13. Click on the original ‘Append Nested’ button below the hr tag
  14. Notice the fields are cleared of their default values

Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-usefieldarray-nested-arrays-forked-cg4yj?file=/src/index.js

USING v7.6.2


Expected behavior Should work exactly as the v7.0.0 example without errors


Desktop (please complete the following information):

  • OS: Mac OSX
  • Browser: chrome, firefox
  • Version 7.6.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mason-smithcommented, May 24, 2021

@bluebill1049 You rock, thanks for tackling this!

1reaction
peakercopecommented, May 21, 2021

I’m sorry it was my mistake. I was using prepend to add element with nested array. With setValue it works ok

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-hook-form useFieldArray with nested array
or I need to make append to the nested useFieldArray from the top-level (parent) component where the child array index is not known....
Read more >
JavaScript Immutability – Frozen Objects in JS Explained with ...
When an object is immutable, you can't add a new property to it, modify it, or delete an existing property. There is no...
Read more >
Typeerror: Todos.Slice Is Not A Function - ADocLib
fix #5246 useFieldArray with nested arrays throws error (#5251) fix #5262 register ... try to append/remove a field, I get. data.slice is not...
Read more >
<FieldArray /> | Formik
<FieldArray /> is a component that helps with common array/list manipulations. ... For the nested field errors, you should assume that no part...
Read more >
react hookform Code Example - Code Grepper
const onSubmit = data => console.log(data); ... {errors.email.message} ... visual studio appsettings development json not nested appsettings.json ...
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