this.state={
task : "",
the_array : []
};
}
handleTextChange=(some_task) =>{
this.setState({
task : some_task
});
}
pushToList = () => {
let taskitem = {
name: this.state.task,
};
this.setState({
the_array: [...this.state.the_array, taskitem],
});
}
render() {
return(
<View style={{backgroundColor:'powderblue', height:'100%'}}>
<FlatList data = {this.state.the_array}
renderItem={(item) => <Text>{item.name}</Text>} keyExtractor={(item) => item.name} >
</FlatList>
<TextInput style={{ backgroundColor: 'lightsteelblue', height:60, borderRadius : 25, marginBottom:20}}
onChangeText={this.handleTextChange}>
</TextInput>
<TouchableOpacity style={{
backgroundColor: 'mediumpurple', height: 60, width: 80, alignSelf: 'center', borderRadius: 20, justifyContent: 'center',
alignItems: 'center', marginBottom:20
}} onPress={this.pushToList}>
这是我的代码。我正在尝试将文本输入内容添加到Flatlist中。为此,我在我的按钮onPress方法(‘pushToList’)中定义了一个名为'taskitem‘的对象,并为它设置了一个名为'name’的属性。“pushTolistMethod”应该把“名字”放到屏幕上的平面列表中。但奇怪的是,它不工作,当我按下按钮时没有任何反应。我想知道有没有人能帮我。
发布于 2019-10-22 13:56:02
你能像这样替换你的扁平列表代码并尝试吗?
<FlatList data = {this.state.the_array}
renderItem={({ item }) => <Text>{item.name}</Text>} keyExtractor={(item) => item.name} >
</FlatList>
数据在项目键上,所以我们使用解构从函数中访问它。
https://stackoverflow.com/questions/58505796
复制相似问题