Alert.alert not working in React native iOS, but perfectly fine in Android

Viewed 4736

Please check the code ,

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}
    />
)}

This snippet will work fine in android but not showing in iOS. why ?

Nb :- this is the most of the code that I can share right now , Edited the code please check it now and let me know if you have any questions.

5 Answers

I don't exactly know what happened, There were also a model component which I used for showing custom loading, after removing the model component the alerts starts working.

Replace alert code with below

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 }
      );
    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);

So in my case, I was not using any custom Modal because for custom Modal the solution is simple to wait until the Modal is closed but, I was using the react-native Spinner component, and I was hiding it properly using the visible={loading} prop and yes, the issue was also not resolved after setting cancelable: false in the Alert.alert config and when I increased the timeout to 5000 it worked even without defining cancelable: false in the Alert.alert config but this a lot of timeout was not good for UX so I quickly checked out the Spinner component props and I came to know that it does have cancelable?: boolean | undefined prop so now when I used Spinner component like below it worked even without timeout.

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

TLDR: use cancelable={true} prop with Spinner component.

Related