How to handle array of fields
See original GitHub issueHi, so say my user API had a nested array like this:
{
id: string,
email: string,
pets: [
{ type: string, name: string }
]
}
Is there a way to implement this kind of form, where i’ll be able to add 0-many pets?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:16 (4 by maintainers)
Top Results From Across the Web
useFieldArray - Simple React forms validation
Each useFieldArray is unique and has its own state update, which means you should not have multiple useFieldArray with the same name ....
Read more >Field Arrays Example - Redux Form
This example demonstrates how to have arrays of fields, both an array of one field or of a group of fields. In this...
Read more >How to get values from html input array using JavaScript
To access all the values entered in input fields use the following method: var input = document. getElementsByName('array[]'); The document.
Read more >get array data into input fields - javascript - Stack Overflow
You can try like this. You can add any input field with the array of object data. I have separated each array item...
Read more >Making dynamic form inputs with React - Gosha Arinich
Among other fields, a company has a variable number of shareholders. Typically when dealing with array inputs, here are the things we can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, you can create a
mapPropsToValues
function that would flatten the array and prefix/suffix each item.This results in
props.values
that’s equivalent to:There are many ways to then display the pets. Using
Object.keys
andfilter
what I usually use in this situation. However, it really depends on your requirements. In the case of arbitrarily adding pets, you’ll need to useprops.setValues
which allows you to declaratively callsetState
on just the key ofFormik.state.values
. This will modifyprops.values
. If you need to rearrange or delete pets arbitrarily, you would not use the pets array index, but rather a uuid to uniquely identify each pet in the flattened state tree. Anyways, here’s the simplest case (when the number of pets doesn’t change).Hope that helps.
What about arrays with useFormik? I have already used useFormik and now kinda confused of how to make arrays of components from initialValues, the values are updating but the component doesn’t. Please help