Alert.aalert在React原生iOS中不起作用,但在Android中完全可以



请检查代码,

import { 
  Alert,
} from 'react-native';
 checkForSendingOtp = () => {
    let hash = 'aBcDeGgd';
    Platform.OS === 'android'
     ? RNOtpVerify.getHash()
        .then(text => {
          hash = text + ''.replace('[', '');
          hash = hash + ''.replace(']', '');
        })
        .then(() => {
          this.sendDataForOtp(hash);
        })
        .catch(console.log)
    : this.sendDataForOtp(hash);
  };
sendDataForOtp(hash) {
axios.post(url,{hash:hash}).then(response=>{
  Alert.alert(
    'Login Failed',
    'Multiple Logins Are Found. n Logout From All Other Devices to Continue.',
    [
      {
        text: 'Proceed ?',
        onPress: () => {}                       
      },
      {
        text: 'No',
        onPress: () => {},
      },
    ],
    {cancelable: false},
   );
  });
}

render() {
   return (
    <Ripple
        style={{
           position: 'absolute',
           top: 0,
           bottom: 0,
           left: 0,
           right: 0,
              }}
        onPress={this.checkForSendingOtp}
    />
)}

这个片段在android中运行良好,但在iOS中不会显示。为什么?

Nb:-这是我现在可以分享的大部分代码,已经编辑了代码,请现在检查一下,如果你有任何问题,请告诉我。

我不知道发生了什么,还有一个模型组件,我用来显示自定义加载,删除模型组件后,警报开始工作。

用以下替换警报代码

Alert.alert(
        'Login Failed',
        'Multiple Logins Are Found. n Logout From All Other Devices to Continue.',
        [
          {
            text: 'Proceed ?',
            onPress: () => {}
          },
          {
            text: 'No',
            onPress: () => {},
            style: 'cancel'
          }
        ],
        { cancelable: false }
      );

这可能与此问题有关:https://github.com/facebook/react-native/issues/10471

出于一些奇怪的原因,将Alert调用为";setTimeout";函数使其工作。但这显然不是避免这个问题的最佳方式。您也可以尝试此解决方案:https://github.com/facebook/react-native/issues/10471#issuecomment-513641325

 setTimeout(() => {
    Alert.alert(
      "title",
      "content",
      [
        {
          text: "ok",
          onPress: () => {
          },
        },
      ],
    )
  }, 10)
    setTimeout(() => {      
        //TODO  use return and setTimeout           
        return Alert.alert(
        i18n.t('notAtYard.alertTitle'),
        'Invalid username or password',
        [
            {
                text: 'Ok',
                onPress: () => {
                    setUserPassword('');
                    setUsername('');
                },
            }
        ],
        {cancelable: false},
    );
    }, 100);

所以在我的情况下,我没有使用任何自定义Modal,因为对于自定义Modal,解决方案很容易等到Modal关闭,但是,我使用的是react-native Spinner组件,并且我使用visible={loading}道具正确地隐藏了它,是的,在Alert.alert配置中设置cancelable: false 后,这个问题也没有得到解决,当我将超时增加到5000时,即使没有在Alert.alert配置中定义cancelable: false,它也能工作,但这段时间的超时对用户体验不好,所以我很快检查了Spinner组件道具,我知道它确实有cancelable?: boolean | undefined道具,所以现在当我像下面这样使用Spinner组件时,它甚至还能工作没有超时。

<Spinner
    visible={loading}
    cancelable={true}
/>

TLDR:使用带有Spinner组件的cancelable={true}道具。

最新更新