Skip to main content

React Native Alert

React Native Alert is an API which is used to display an alert dialog with specified title and message. It uses an alert() method to prompt an alert dialog. This Alert dialog provides three different lists of buttons (combination of neutral, negative, and positive) to perform action. Pressing any of these buttons calls the onPress callback method and dismiss the alert. By default, Alert has only a positive (OK) button.
The Alert that prompts to enter some information is created using AlertIOS. It is only supported in iOS.

React Native Alert in iOS

In iOS, we can specify number of buttons. Each buttons can be separately specify a style, which is one of default, cancel, or destructive.

React Native Alert in Android

In Android, the Alert button can be specified with mainly three buttons. These buttons are a neutral, negative and a positive button:
Alert buttons are specified in three different combinations. These are:
  1. If we specify only one button, it will be the 'positive' one (such as 'OK').
  2. If we specify two buttons, it will be 'negative' and 'positive' (such as 'Cancel', 'OK').
  3. Specifying three buttons means 'neutral', 'negative', 'positive' (such as 'Later', 'Cancel', 'OK').
In Android, the alerts are by default dismissed by clicking outside the alert dialog. The dismiss event can be handled by using an optional parameter, with an onDismiss callback property { onDismiss: () => {} }. However, we can disable the dismissing property by using the property {cancelable: false}.

React Native Alert Example

In this example, we create two alerts on two buttons. One alert contains two options button without cancelable. Other alert contains three options button with cancelable false.

App.js

  1. import React, { Component } from 'react';  
  2. import { Alert, AppRegistry, Button, StyleSheet, View } from 'react-native';  
  3.   
  4. export default class ButtonBasics extends Component {  
  5.     showAlert1() {  
  6.         Alert.alert(  
  7.             'Alert Title',  
  8.             'My Alert Msg',  
  9.             [  
  10.                 {  
  11.                     text: 'Cancel',  
  12.                     onPress: () => console.log('Cancel Pressed'),  
  13.                     style: 'cancel',  
  14.                 },  
  15.                 {text: 'OK', onPress: () => console.log('OK Pressed')},  
  16.             ]  
  17.         );  
  18.     }  
  19.     showAlert2() {  
  20.         Alert.alert(  
  21.             'Alert Title',  
  22.             'My Alert Msg',  
  23.             [  
  24.                 {text: 'Ask me later', onPress: () => console.log('Ask me later pressed')},  
  25.                 {  
  26.                     text: 'Cancel',  
  27.                     onPress: () => console.log('Cancel Pressed'),  
  28.                     style: 'cancel',  
  29.                 },  
  30.                 {text: 'OK', onPress: () => console.log('OK Pressed')},  
  31.             ],  
  32.             {cancelable: false}  
  33.         )  
  34.     }  
  35.     render() {  
  36.         return (  
  37.             <View style={styles.container}>  
  38.                 <View style={styles.buttonContainer}>  
  39.                     <Button  
  40.                         onPress={this.showAlert1}  
  41.                         title="Button 1"  
  42.                     />  
  43.                 </View>  
  44.                 <View style={styles.buttonContainer}>  
  45.                     <Button  
  46.                         onPress={this.showAlert2}  
  47.                         title="Button 2"  
  48.                         color="#009933"  
  49.                     />  
  50.                 </View>  
  51.             </View>  
  52.         );  
  53.     }  
  54. }  
  55.   
  56. const styles = StyleSheet.create({  
  57.     container: {  
  58.         flex: 1,  
  59.         justifyContent: 'center',  
  60.     },  
  61.     buttonContainer: {  
  62.         margin: 20  
  63.     },  
  64.     multiButtonContainer: {  
  65.         margin: 20,  
  66.         flexDirection: 'row',  
  67.         justifyContent: 'space-between'  
  68.     }  
  69. })  

Output:

React Native Alert React Native Alert React Native Alert

Comments

Popular Posts

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...