Formik: Pushing Arrays of Object in FieldArray Helpers

Created on 11 Oct 2018  路  7Comments  路  Source: formium/formik

Any suggestion how can I push arrays of object?

stale

Most helpful comment

Check this link: https://codesandbox.io/s/4k92x4vw0

The default rows have 5 field array. Upon clicking the Add button i need to add 2 more rows.
I tried to push an object twice but it only adds 1 row.

 onClick={() => { 
push({templateData}); 
push({templateData});
}}

All 7 comments

A little bit more detail would help us help you :)

Check this link: https://codesandbox.io/s/4k92x4vw0

The default rows have 5 field array. Upon clicking the Add button i need to add 2 more rows.
I tried to push an object twice but it only adds 1 row.

 onClick={() => { 
push({templateData}); 
push({templateData});
}}

It seems to me like the two don't happen synchronously and so one of them gets swallowed basically. It worked for me after I added async/await to it. See here: https://codesandbox.io/s/n9n43p3pj0

I see.
Actually, in my actual project, i need to add 10 rows. It would be great if I can push an array of objects . Instead of adding push X times

example
push([{ product: "books" }, { product: "clothes" }] , { product: "clothes" }], { product: "clothes" }])

Thanks @Saifadin

Ahh okay, but you could also do something like this:

onClick={() => {
  const countArray = new Array(10);
  countArray.forEach(() => push({ product: '', price: '' }));
}}

You create an Array with length 10 and then you loop through them and you will create for each element a push.

Worked for me after I added async/await from the beginning

onChange={async e => 

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pmonty picture pmonty  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments

Jucesr picture Jucesr  路  3Comments

dfee picture dfee  路  3Comments

ancashoria picture ancashoria  路  3Comments