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.

Insert from useFieldArray doesn't work as expected

See original GitHub issue

Describe the bug Clicking INSERT button for first FieldArray - useFieldArray1 inserts properly a new FieldArray between useFieldArray1 and useFieldArray2 but value nestedArray is [{ field1: "field21", field2: "field22" }] (so the same as second FieldArray - useFieldArray2 ) instead of [{ field1: "abc", field2: "123" }].

To Reproduce Case 1:

  1. Click INSERT for first FieldArray - useFieldArray1
  2. Get nestedArray = [{ field1: "field21", field2: "field22" }]

Case 2:

  1. Click INSERT for second FieldArray - useFieldArray2
  2. Get empty nestedArray. No nested fields are rendered

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

Expected behavior Case 1, 2: nestedArray should be equal to return of getFieldArrayValues function from example - insert(index + 1, getFieldArrayValues()) - in this case [{ field1: "abc", field2: "123" }] and these values should be rendered.

Desktop (please complete the following information):

  • OS: macOS
  • Browser chrome

Additional context

  • If I change controlled inputs to uncontrolled then in both cases I get empty nestedArray
  • Sometimes I get error: data.slice is not a function or its return value is not iterable but after a few page refreshes it starts to work

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
DawidNazarkocommented, Jul 15, 2020

I updated codesandbox.

Initial state: image

Clicking first INSERT (next to useFieldArray1) gives: image

Clicking second INSERT (next to useFieldArray2) gives: image

0reactions
bluebill1049commented, Jul 17, 2020

if you want to insert, why not just insert API?

Read more comments on GitHub >

github_iconTop Results From Across the Web

React UseHookForm set focus when using useFieldArray
What ended up working was adding a setTimeout to the end of the useEffect call. useEffect(() => { // other useEffect code setTimeout(() ......
Read more >
React Hook Form V7 with Material UI and Typescript Tutorial
In this video, I will help in understanding useFieldArray to create dynamic lists or tables.Link to starter project to follow ...
Read more >
API Documentation - Simple React forms validation
When set to true (default) and the user submits a form that fails the validation, it will set focus on the first field...
Read more >
useForm - unregister - React Hook Form
The example below shows what to expect when you invoke the unregister method. ... By unregister an input, it will not affect the...
Read more >
react-hook-form - Bountysource
If form includes optional array field, useFieldArray will return with type ... a value in the multiselect both Reactstrap and native select doesnt...
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