如何在 Alert 函数 [React-Native] 上从 onPress 调用方法
<Button
onPress={{() => Alert.alert(
'Alert Title',
'alertMessage',
[
{text: 'Cancel', onPress: () => console.log('Cancel Pressed!')},
{text: 'OK', onPress: () => {this.onDeleteBTN}},
],
{ cancelable: false }
)}}
>
<Text> Delete Record </Text>
</Button>
在 Alert Dialog 上的 OK 按钮之后 我要打电话
onDeleteBTN = () => {
alert(' OnDelete');
}
{text: 'OK', onPress: () => {this.onDeleteBTN.bind(this)}},
{text: 'OK', onPress: () => {this.onDeleteBTN}},
不行
最佳答案
第一期,Button
组件有一个 title
prop 而不是 <Text>
作为一个 child 。第二个问题是你有一堆语法错误并且没有正确调用函数(或绑定(bind))。如果你修复了它,那么它应该可以正常工作;例如:
alert = (msg) => {
console.log(msg)
}
onDeleteBTN = () => {
this.alert(' OnDelete')
}
render() {
return (
<View style={styles.container}>
<Button
title="Delete Record"
onPress={() => Alert.alert(
'Alert Title',
'alertMessage',
[
{text: 'Cancel', onPress: () => console.log('Cancel Pressed!')},
{text: 'OK', onPress: this.onDeleteBTN},
],
{ cancelable: false }
)}
/>
</View>
);
}
注意:
- 我不知道你的
alert()
功能应该做的,所以我做了一个虚拟的记录到控制台。 - 还有其他方法可以做到这一点,比如调用
onDeleteBTN()
或绑定(bind)。
关于javascript - 如何在 Alert 函数 [React-Native] 上从 onPress 调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44527230/