【发布时间】:2019-09-03 19:38:21
【问题描述】:
我有一个formik 表单,它根据数组x 的长度将View 映射到两个inputs。 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 输入,则文本会填充description 的所有其他迭代,我该怎么做这份工作?
看看小吃
【问题讨论】:
标签: javascript arrays reactjs react-native mapping