question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rendering form with array of objects

See original GitHub issue

I have the following problem:

I’m trying to render multiple input fields based on index such as:

{values.education.map((item, index) => getInputFields(index))}

where values contains an array of objects as:

education: [ 
{
what: '',
where: '',
description: '',
startDate: '',
endDate: ''
} 
]

and getInputFields(index) renders a set of input fields based on the education index.

The problem im running into is that if i use the default handleChange from formik, the values object i get is:

{
education[0].what: ''
education[0].where: '',
education[0].description: '',
education[0].startDate: '',
education[0].endDate: ''
}

So i decided to implement a custom onChange handler that uses the setFieldValue to manually set education. It works up to a point. Now my errors and touched objects are not working. Errors return the exact same object as before:

{
education[0].what: 'This field is required.'
education[0].where: 'This field is required.',
education[0].description: 'This field is required.',
education[0].startDate: '',
education[0].endDate: ''
}

Is there a special way to handle a form with an array of objects?

Will this feature be implemented anytime soon in formik?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
andymac4182commented, Sep 25, 2017

Something similar to this would be great https://redux-form.com/7.0.4/docs/api/fieldarray.md/

4reactions
rcampos87commented, Sep 4, 2017

my input names do use unique names education[{index}].what etc… it would be a not-so-elegant solution to convert a flat list into a nested structure… but if thats the only way…

if formik would use path instead of field name, itd be so helpful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to render an array of objects in ReactJS ? - GeeksforGeeks
How we will render an Array of Objects? We will use the map function to access each object from the array. The map()...
Read more >
Render an array of objects react - Stack Overflow
When im trying to add new object console gives me this error: Uncaught Error: Objects are not valid as a React child (found:...
Read more >
How to render array of objects in HTML elements ... - YouTube
How to render an array of objects in HTML element using javascript using map function (without React).
Read more >
React State Array of Objects - YouTube
Learn how to use useState to keep track of arrays of objects in a react app.
Read more >
How to render an array of objects with Array.map in React
The Array.map() method is key to rendering objects and data in react and it is a prototype function on all arrays in JavaScript....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found