const [formSectionData, setFormSectionData] = useState([ {id: 1, text: ''}, {id: 2, text: ''}, ]); const addSection = section => { let existingForm = [...formSectionData]; existingForm.push(section); setFormSectionData(existingForm); }; const onTextChange = (text, index) => { let existingForm = [...formSectionData]; existingForm[index].text = text; setFormSectionData(existingForm); }; return ( <SafeAreaView> {formSectionData.map((item, index) => { return ( <View style= key={index}> <TouchableOpacity onPress={() => addSection(item)}> <Text>+ Add Section</Text> </TouchableOpacity> <TextInput key={index} placeholder={'Enter value' + index} value={item.text} onChangeText={text => onTextChange(text, index)} /> </View> ); })} </SafeAreaView>);
I'm trying to create a new input field using addsection, But now when I'm enter a value to the input field same value will appear in app its original input fields, How to properly add a new input field from the existing json
https://stackoverflow.com/questions/66811242/creating-dynamic-form-its-values-change-across-all-the-copy-fields March 26, 2021 at 12:53PM
没有评论:
发表评论