...

/

The FieldArray Component

The FieldArray Component

Learn how to use the FieldArray component to do common array manipulations on a form's state.

Sometimes, we might want to collect a value multiple times, but prior to collecting the values we don’t know the exact number of times we want to collect them. For example, if we ask our users to enter the names of their friends, we may not know how many friends they have, so we want to give them the flexibility to add and remove as many friends as they would like. We’ll save all the values in a single array. This requires us to do a lot of array manipulation that can quickly get complex. Formik provides a component that helps us easily manipulate arrays—the FieldArray component.

The FieldArray component

The FieldArray component accepts a name prop, which is the key in initialValues that has the array we are interested in. Then, like the Field and Formik components, the FieldArray component gives us access to some helper methods using render props. We will use these methods to do some manipulations on the array.

To see this in action, we’ll create a form that will display a field and ask the user to enter a ...