react-hook-form: V7 bug - nested field array has incorrect value appended and exception thrown on future append

Describe the bug This one might make your head spin a little. There are two issues here related to nested field arrays.

Let’s say we have a form values object like:

{
   "first":[
      {
         "second":[
            {
               "third":[
                  {
                     "fourth":{
                        "objectKey":"someValue"
                     }
                  }
               ]
            }
         ]
      }
   ]
}

Then there will be some issues if we try to append the middle values of the field array.

  1. If we append a middle field array element (let’s say second) to the first array and we pass the final value (fourth) as an object, then it will be appended as undefined rather than the object we specified.
{
   "second":[
      {
         "third":[
            {
               "fourth":{
                  "objectKey":"someValue"
               }
            }
         ]
      }
   ]
}
  1. If we try to append a middle element after we’ve appended a previous middle element, then the operation will throw an exception and crash our application. So for example we append the value above and then append.
{
   "third":[
      {
         "fourth":{
            "objectKey":"someValue"
         }
      }
   ]
}

to the previously appended element (second) this will throw an exception.

To Reproduce Steps to reproduce the behavior:

  1. Go to the code sandbox
  2. Immediately observe via the watch output that the newly appended value has undefined as a final value rather than the object. Notice the default value passed to useForm does have the correct value
  3. Click show elements
  4. Click the second add button that appears (add for the newly created field array element) and notice the exception that is thrown.

Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-usefieldarray-template-forked-qc0ye?file=/src/index.js:1120-1181

I’m not sure if there is a limit to nesting, but if there is I would hope it could go more than 3-4 levels deep.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (17 by maintainers)

Commits related to this issue

Most upvoted comments

No rush just wondering. Thank you!

Sounds good. Appreciate all the help here.

Ok, thanks. Yes, this seems to be working correctly.