Problem with Array with multiple nested arrays #7737
-
Hi, I was wondering if anyone could help me with a problem. I'm building a form that receives data from an array that has the following structure:
however when giving console in the useForm control it reads only the first array, the form is being rendered correctly, the problem is in the value of each input, because it is only reading the data from an array. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 26 replies
-
Can you provide a codesandbox for your use-case? |
Beta Was this translation helpful? Give feedback.
-
So, there're a couple of issue with what you're trying to do.
"dependencies": {
"react-hook-form": "7.22.5"
}, but the way you're using the render={({ value, onChange }) => (
<input value={value} onChange={onChange} />
)} Since V7, the API for render={({ field }) => (
<input value={field.value} onChange={field.onChange} />
)} Please, refer to the RHF V7 docs to learn more about the current API
const DATA = [
[
{
type: "ADULT"
}
],
[
{
prop: "ADULT"
}
]
] You gotta extend it to something like this: const defaultValues = {
rooms: [
{
number: 101,
guests: [
{ type: "ADULT", firstName: "", lastName: "" },
{ type: "ADULT", firstName: "", lastName: "" }
]
},
{
number: 102,
guests: [
{ type: "ADULT", firstName: "", lastName: "" },
{ type: "ADULT", firstName: "", lastName: "" }
]
}
]
}; Note the
I've created this CSB with the concepts I just talked about above. Please, take a look and don't hesitate to ask for clarifications |
Beta Was this translation helpful? Give feedback.
-
{
|
Beta Was this translation helpful? Give feedback.
So, there're a couple of issue with what you're trying to do.
but the way you're using the
<Controller />
is from V6Since V7, the API for
<Controller />
has changed and now requires you to access your field value and handlers from thefield
property returned fromrender
Please, refer to the RHF V7 docs to learn more about the current API