如何防止 React Native 中的多个警报?

How to prevent multiple Alerts in React Native?

有没有办法在发送另一个 Alert.alert() 之前判断屏幕上是否已经存在?

我有这个功能:

CheckInternet(){
  if(this.props.json.undefined){
    Alert.alert("Check your internet connection");
  }
}

ComponentDidUpdate(){
  this.CheckInternet();
}

问题是我在那个函数里面还有其他事情要做,我只是写了相关的代码,所以我不能把 CheckInternet 函数放在 ComponentDidUpdate 之外。

问题是组件在获取 json 后更新了两次,因此发送了两次该警报。我想通过使用让我知道屏幕上是否已经有警报的条件来防止同时出现两个警报。我似乎没有在警报文档中找到类似的内容。有什么想法吗?

试试这个:

CheckInternet(){
    if (!this.alertPresent) {
        this.alertPresent = true;
        if (this.props.json.undefined) {
            Alert.alert("", "Check your internet connection", [{text: 'OK', onPress: () => { this.alertPresent = false } }], { cancelable: false });
        } else {
            this.alertPresent = false;
        }
    }
}

ComponentDidUpdate(){
  this.CheckInternet();
}