2

I am using Alert.alert() for displaying alert in react native ios. Problem i am facing is that if tow alert of same message appears they appear one above the other.

While in android second alert appear after dismissing the first one.

Anyway to make it behave like android in ios?

Deepak Verma
  • 373
  • 7
  • 19

1 Answers1

0

You can add a certain state boolean to check if alert is visible or not such as

  state = {
    alertBoxVisible: false,
  }

  onAlertShow = () => {
    if(!this.state.alertBoxVisible) {
      this.setState({alertBoxVisible: true}, () => {
        Alert.alert(
          'Alert Title',
          'My Alert Msg',
          [
            {text: 'OK', onPress: () => this.setState({alertBoxVisible: false})},
          ],
          { cancelable: false }
        )
      })
    }
  }

and use async await to chain the functions that call the onAlertShow such as

onPress={async () => {
  await this.onAlertShow()
  await this.onAlertShow()
}}
Pritish Vaidya
  • 21,561
  • 3
  • 58
  • 76
  • Good workaround, but i'm expecting solution which doesn't include dealing with state. Can't something be done by passing some props to native alertview? – Deepak Verma Sep 14 '18 at 10:49
  • 1
    The native solution has been answered [here](https://stackoverflow.com/a/10228568/6606831) by checking the visibility of the `AlertBox`. I don't know about native code but you can try implementing one of these solutions – Pritish Vaidya Sep 14 '18 at 10:51