useFieldArray with nested arrays throws error with `append` and `remove` in nested array (data is not iterable)
See original GitHub issueDescribe 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:
- Go to https://codesandbox.io/s/react-hook-form-usefieldarray-nested-arrays-forked-cg4yj?file=/src/index.js
- Click on ‘Append Nested’ button
- Notice the fields are not populated with default values
- Click on ‘Delete Nested’ button
- Observe thrown error
- Clear the error and refresh the page
- Click on ‘Append Nested’ button
- Click on the new ‘Append Nested’ button that appears above the
hr
tag - Observe error
- Clear the error and refresh the page
- Click the ‘Append’ button
- Click on the new ‘Append Nested’ button that appears above the
hr
tag - Click on the original ‘Append Nested’ button below the
hr
tag - 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:
- Created 2 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top GitHub Comments
@bluebill1049 You rock, thanks for tackling this!
I’m sorry it was my mistake. I was using
prepend
to add element with nested array. WithsetValue
it works ok