代码之家  ›  专栏  ›  技术社区  ›  Shingai Munyuki

如何编辑动态添加的输入,而不让输入数据填充该数组中的其他输入?

  •  0
  • Shingai Munyuki  · 技术社区  · 7 年前

    我有一个 formik View 有两个 inputs x . x的长度将根据不同的场景而变化。

    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>
        );
      }
    }
    

    当我提交时,我无法使它正常工作。当我键入时,文本也会填充该名称的所有其他输入,例如,如果我键入 description 输入时,文本将填充 描述

    看一看小吃

    HERE

    0 回复  |  直到 7 年前
        1
  •  1
  •   etarhan    7 年前

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

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

    然后更改动态添加的TextInputs以使用以下值:

    {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>
      );
    })}
    

    退房 my fork of your CodeSandbox 一个工作演示。

    推荐文章