react-hook-form: getValues('fieldArray') doesn't seem to work inside of event handler

Describe the bug When using useFieldArray, getValues('fieldArray') returns undefined inside of an event handler. But getValues().fieldArray returns the array though. 😅

To Reproduce Steps to reproduce the behavior:

  1. Click on ‘Append’.
  2. See log in the console.

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

Expected behavior getValues('fieldArray') should returns the same thing as getValues().fieldArray.

About this issue

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

Most upvoted comments

@bluebill1049 Hm. I, by the way, also put “inline” defaultValue on each <Controller /> in my program, but it’s got the same inconsistency as illustrated in my CSB above (not sure if this is what you mean by “inline default value”). What makes it consistent in the end is to do defaulting values by using watch('test', fields) you mentioned above in my child component. 🤩

But then again, unfortunately I couldn’t achieve the same consistency when using useWatch({ name: 'test', control, defaultValue: fields }). 😕 Here, you can see what I mean by uncommenting the useWatch block: https://codesandbox.io/s/react-hook-form-usefieldarray-template-forked-do236?file=/src/Child.jsx:132-248

And yup - at the end of the day, probably this really needs some good explanation and tutorial 😁

I will take a look this useWatch inconsistency tmr.

Sure! 😃 Would be glad to try to reconstruct this on CSB. I’ll work on it sometime this week~

on now is: should I use fieldAr

sorry to hear that 🙏

fieldArray.length is the right one, let me explain what’s happening, with uncontrolled input, those form value states live inside the inptus, which the reason why you need watch('fieldArray', fields), this goes to the same as watch need a defaultValue, otherwise it will return undefined when you try to watch input, because the input is not even rendered yet.

Ahh okay, didn’t know that it was expected. Thanks, as always, Bill! 😃)