Question about dynamic fields in ArrayField Nested Form approach
Hi Joe,
I am trying ArrayField to generate dynamic fields using custom component inputs.
I added 3 sets of fields and filled all the fields. When I tried to remove the second one, the third one was also removed.

Here is some part of my code.
https://gist.github.com/KLC215/34f8ecf60488b3670fc5a35f4015f2a1
MainPage.jsis the usage partMatrixDynamic.jsrenderFields(fields)is the render part
I tried to use copy this example
https://joepuzzo.github.io/informed/?path=/story/arrays--nested-form
and replaced origin inputs to my custom input, it works as expected.
So my question is, What is the best practice / correct way to render dynamic fields inside ArrayField?
Thanks.
Try to use my key prop directly and not modify the key. Also try to simplify the code down to its most basic form, get it working, then add back complexity until it fails. This should work fine. If u can create a code sandbox and recreate issue there it would help but I believe it has somthing to do with how the component is rendering
But this seems to be a key issue if i were to guess
You get it fixed ?
Oh....sorry. I just saw these messages.
I will get it a try now.
If I still stuck on it, I will create a code sandbox to simulate the case.
Thanks for your solution :)
No problem. I do think its really a Key issue. One thing to try is add two fields and then delete one. If all the values after the one you delete go away its def an issue with the key prop.
Too elaborate in your example you had three fields. Try to add four and see how it reacts after deletion.
Yes. It is a key issue and my mistake.
For my code, I iterated rows and iterated each cell inside 1 row.
Before:
I set the original keys to each cell but not the row.
Fix:
I set the original keys to each row only. and it seems that I cannot add any keys in each cell, so I remove the keys in each cell.
It can fix the issue.
Thank you so much~~~
Awesome glad I could help
Oops....Sorry...
After I applied the multi-step form approach, I found that the ArrayField is not working correctly.
When I add 4 rows and delete the second row.
The UI works fine, but the last row has been duplicated in values.

I have updated the code to replicate the issue. https://stackblitz.com/edit/react-kw8ntc
It is basically the same steps in https://joepuzzo.github.io/informed/?path=/story/multistep-forms--complex-multistep
I added a ArrayField with my PanelDynamic UI wrapper in step EpiPen.
Also, I added keepState and relevant on both ArrayField and Fields inside ArrayField.
Is it a correct way to do it?
<ArrayField field="friends" relevant={relevant} keepState>
{props => (
<PanelDynamic {...props}>
{({ field, key, index, initialValue, remove }) => (
<div key={key}>
<h5>Panel {index}</h5>
<Text
field={`${field}.name`}
initialValue={initialValue && initialValue.name}
placeholder="Name"
relevant={relevant}
/>
<Text
initialValue={initialValue && initialValue.age}
field={`${field}.age`}
placeholder="age"
relevant={relevant}
/>
<button onClick={remove}>Remove</button>
</div>
)}
</PanelDynamic>
)}
</ArrayField>
keep state on the array field and not the fields themselves and added key prop to div. Works now
Wait no that still has issue i need to look into this
Ok so the code you had is fine! I had a bug that is now fixed! Update to the latest release
Just make sure you have keepState on the ArrayField and the Fields you want to keep
It seems that the issue is still happen.
This time, when I add 4 panels and remove the 2nd panel, the last panel value will be gone, and the 3rd panel value will be duplicated.

I have added key to div inside the PanelDynamic.js.
renderPanels = () => {
const { fields, children, relevant } = this.props;
return fields.map(({ field, key, remove, initialValue }, i) => (
<div
key={key}
style={{ backgroundColor: "#FFFFE0", padding: 32, marginBottom: 24 }}
>
{children({ field, index: i + 1, initialValue, remove })}
</div>
));
};
and also I have updated the informed version to 3.5.1: https://stackblitz.com/edit/react-kw8ntc
Update to the latest release! I fixed this issue yesterday
3.5.2
Sorry about that.....didn't check the update. 😄
Yeah~ Above issue is fixed, Thank you.
But I just found another issue.

I add 4 panels and remove all 4. When I re-add 4 panels, it seems that the values are still be saved. https://stackblitz.com/edit/react-kw8ntc
Sorry to bother you again. 😅
I'm using version 3.6 and facing the same problem like this issue mentioned. Is there any update on this issue? Many thanks.
Check that you are passing the key correctly. I bet its the same issue as originally