因此,我正在学习react native中的平面列表,并尝试编写一个函数,通过在按钮中使用onlongpress删除平面列表中的项目,这是终端中显示的项目示例:
Array [
Object {
"Friday": false,
"Monday": true,
"Saturday": false,
"Sunday": false,
"Thursday": false,
"Tuesday": false,
"Wednesday": false,
"key": 0.08631781113770953,
"workoutName": "",
},
]
这是我正在使用的函数,我根据教程得到:
removeItem(item){
this.setState({
workoutList: this.state.workoutList.filter((item)=>item.key !== item.key)
});
}
这就是我调用函数的地方:
openTwoButtonAlert = () => {
Alert.alert(
'Delete Workout',
'Are you sure to delete this Workout?',
[
{text: 'Delete', onPress: this.removeItem(item)},
{text: 'Cancel',
style: 'cancel',
},
],
);
}
这是我的舞蹈家:
<FlatList
data={this.state.workoutList}
keyExtractor={(item, index) => item.key.toString()}
renderItem={(data) => (
<View style={{padding:20}}>
<TouchableOpacity onPress={() => this.props.navigation.navigate("WorkoutCreated", {
workoutName: data.item.workoutName,
Monday: data.item.Monday,
Tuesday: data.item.Tuesday,
Wednesday: data.item.Wednesday,
Thursday: data.item.Thursday,
Friday: data.item.Friday,
Saturday: data.item.Saturday,
Sunday: data.item.Sunday,
})}
onLongPress={this.openTwoButtonAlert()}>
<Text>{data.item.workoutName}</Text>
</TouchableOpacity>
</View>
)}
/>
要更新的状态是workoutlist,我正在使用一个类组件,当我启动代码时,它返回错误: TypeError: undefined is not an object (evaluating 'this.setState')
提前感谢你的帮助。
1条答案
按热度按时间r7knjye21#
这样试试