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
一个工作演示。