我有一个react-native
警报,如下所示。我想实现的是防止警报框消失onPress
和延迟警报框消失。
如何用react-native built in Alert component完成此操作?
我假设有一个内置的preventAlertBoxDismiss()
和dismissAlert()
函数,但是在文档中找不到。
1)防止在按下按钮时关闭警报框
onPressAskMeLater = () => {
preventAlertBoxDismiss();
anyAction();
}
2)延迟警报框关闭
onPressAskMeLater = () => {
preventAlertBoxDismiss();
setTimeOut(3000);
dismissAlert();
}
警报代码
Alert.alert(
'Alert Title',
'My Alert Msg',
[
{text: 'Ask me later', onPress: () => this.onPressAskMeLater()},
{text: 'OK'},
],
{cancelable: false},
);
答案 0 :(得分:1)
系统不支持该方法。它属于本机UI组件。您可以使用该组件来实现对话框。这样,您可以控制对话框的关闭。
同时,您也可以使用第三个库。例如react-native-dialog
。
将其用作以下内容:
<View>
<TouchableOpacity onPress={this.showDialog}>
<Text>Show Dialog</Text>
</TouchableOpacity>
<Dialog.Container visible={this.state.dialogVisible}>
<Dialog.Title>Account delete</Dialog.Title>
<Dialog.Description>
Do you want to delete this account? You cannot undo this action.
</Dialog.Description>
<Dialog.Button label="Cancel" onPress={this.handleCancel} />
<Dialog.Button label="Delete" onPress={this.handleDelete} />
</Dialog.Container>
</View>
....
showDialog = () => {
preventAlertBoxDismiss();
setTimeOut(3000);
dismissAlert();
this.setState({ dialogVisible: true });
};
在showDialog
方法中,您可以延迟对话框的关闭。因为它显示和关闭由您自己的状态控制。