I have a formik form that maps a View with two inputs depending on the length of the array x. The length of x will vary depending on different scenarios.

Here is the code.

import React, { Component, Fragment } from "react";
import { Button, TextInput, View, StyleSheet } from "react-native";
import { Formik } from "formik";

class App extends Component {
  render() {
    const x = [1,2];
    return (
      <View>
        <Formik
          initialValues={{ name: "", description: "" }}
          onSubmit={values => alert(JSON.stringify(values))}
        >
          {({ values, errors, handleBlur, handleChange, handleSubmit }) => (
            <Fragment>
              <TextInput
                placeholder="name"
                value={values.name}
                handleBlur={() => handleBlur("name")}
                onChangeText={handleChange("name")}
                style={styles.input}
              />
              <TextInput
                placeholder="description"
                value={values.description}
                handleBlur={() => handleBlur("description")}
                onChangeText={handleChange("description")}
                style={styles.input}
              />
              {x.map(x => {
                return (
                  <View>
                    <TextInput key={x}
                      placeholder={`name`}
                      value={values.name}
                      handleBlur={() => handleBlur}
                      onChangeText={handleChange}
                      style={styles.input}
                    />
                    <TextInput
                      placeholder={`description`}
                      value={values.description}
                      handleBlur={() => handleBlur(x.toString())}
                      onChangeText={handleChange}
                      style={styles.input}
                    />
                  </View>
                );
              })}
              <Button title="submit" onPress={handleSubmit} />
            </Fragment>
          )}
        </Formik>
      </View>
    );
  }
}

When I submit, I cannot get it to work properly. when I'm typing, the text also fills in all the other inputs of that name, for example, if I'm typing in a description input, the text fills in every other iteration of description, how can I make this work?

Take a look at the snack

HERE

1 Answers

1
etarhan On Best Solutions

You need to dynamically create the initial values object in order for the submit to work properly, for example:

const x = [1, 2];
const initialFields = { name: "", description: "" };
const extraFields = x.map(num => ({
  [`name${num}`]: "",
  [`description${num}`]: ""
}));

This can be passed to your Formik form like this:

<Formik
  initialValues={Object.assign(initialFields, ...extraFields)}
  onSubmit={values => alert(JSON.stringify(values))}
>

Then change your dynamically added TextInputs to use these values like this:

{x.map(x => {
  return (
    <View>
      <TextInput
        placeholder={`name${x}`}
        value={values[`name${x}`]}
        handleBlur={() => handleBlur(`name${x}`)}
        onChangeText={handleChange(`name${x}`)}
        style={styles.input}
      />
      <TextInput
        placeholder={`description${x}`}
        value={values[`description${x}`]}
        handleBlur={() => handleBlur(`description${x}`)}
        onChangeText={handleChange(`description${x}`)}
        style={styles.input}
      />
    </View>
  );
})}

Check out my fork of your CodeSandbox for a working demo.