How To Use Alert With Two Option Using React Native?

Dec 04, 2021 . Admin

react_native_alert_with_two_option_example.png

Hi Guys,

Today, I will learn you how to use alert with two option example. We will check how to use alert with two option. This is a short guide on use alert with two option in react native. Let's get started with how to use alert with two option in react native.

Here, I will give you full example for alert message box with multiple option using react native as bellow.

Step 1 - Create project

In the first step Run the following command for create project.

expo init MyWebtutsProject
Step 2 - App.js

In this step, You will open App.js file and put the code.

import React, { Component } from 'react'; 
import { Alert, Button, View, StyleSheet } from 'react-native';

export default class MyWebtutsProject extends Component {

  twoOptionsAlertFunction = () => {
    Alert.alert(
      'Hello',
      'Alert with two option.',
      [
        { text: 'Yes', onPress: () => console.log('Yes') },
        { text: 'No', onPress: () => console.log('No') },
      ],
      { cancelable: false }
    );
  }

  render() {
    return (
      <View style={styles.container}>
        <View style={{marginVertical: 10}}>
          <Button title='Alert with Two Options' onPress={this.twoOptionsAlertFunction} color="#009933"/>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
  }
});
Step 4 - Run project

In the last step run your project using bellow command.

expo start
Output

It will help you..

#React Native