【发布时间】:2020-12-20 14:16:40
【问题描述】:
我创建了一个按钮,每当被触摸时,它都会将这个对象 {problema: '', solucion: '', key: Math.random(), index: index+1} 添加到这个 useState 数组 const [数据,setData] = useState ([])。常量数据用于包含两个 TextInput 的 Flatlist,一个用于更改字符串问题,另一个用于更改字符串解决方案。我的问题是,当您多次触摸按钮时,常量数据将包含多个对象,我不知道如何根据您使用的 TextInput 更改该特定字符串。
{
let [index, setIndex] = useState(-1);
let passingValue = { problema: '', solucion: '', key: Math.random().toString(), index: index+1 }
const [data, setData] = useState([])
const onPressPlus = () => {
setIndex(index + 1)
setData(prevState => [...prevState, passingValue])
console.log(data);
}
return (
<View>
<TouchableOpacity onPress={onPressPlus}>
<AntDesign name='pluscircle'/>
</TouchableOpacity>
<View>
<FlatList
data={data}
renderItem={itemData => (
<View style={{ flexDirection: 'row', }}>
<View>
<TextInput
placeholder="Escribe tu problema"
placeholderTextColor='black'
value={itemData.item.problema}
onChangeText={(e) => { /* I dont know how to change the value of this specific itemData.item.problema with setData()*/ }}
multiline={true}
numberOfLines={2}
/>
</View>
<View>
<TextInput
placeholder="Escribe tu problema"
placeholderTextColor='black'
value={itemData.item.solucion}
onChangeText={(e) => { /* I dont know how to change the value of this specific itemData.item.solucion with setData() */ }}
multiline={true}
numberOfLines={2}
/>
</View>
</View>
)}
keyExtractor={itemData => itemData.key}
/>
</View>
</View>
)
}
【问题讨论】:
标签: reactjs react-native react-native-flatlist use-state react-native-textinput