9 February 2020

Alert with options in react native

  • Create Components folder under src folder
  • Add AlertDialogEXP1.js file and add below Code
import React from "react";
import { StyleSheetViewButton,Alert } from "react-native";

const AlertDialogEXP1 = () => {
  const ShowAlertDialog = () => {
    Alert.alert("Alert Dialog Title""Alert Dialog Message", [
      {
        text: "Ask me later",
        onPress: () => console.log("Ask me later Button Clicked")
      },
      {
        text: "Cancel",
        onPress: () => console.log("Cancel Button Pressed"),
        style: "cancel"
      },
      { text: "OK"onPress: () => console.log("OK ButtonPressed") }
    ]);
  };
  return (
    <View style={styles.MainContainer}>
      <Button title="Show Alert Dialog " onPress={ShowAlertDialog} />
    </View>
  );
};
export default AlertDialogEXP1;

const styles = StyleSheet.create({
  MainContainer: {
    justifyContent: "center",
    flex: 1,
    margin: 10
  }
});
  • Open App.js file and update code as below
import React from 'react';
import {  Text,View,StyleSheet } from 'react-native';
 import AlertDialogEXP1 from './src/Component/AlertDialogEXP1'

 const  App=()=> {
  return (
    <View style={styles.container}>
      <AlertDialogEXP1/>
    </View>
  );
}
export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    paddingTop:80
  },
});


Output:



No comments:

Post a Comment