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

Reloading UITableView while Animating Scroll in iOS 11

Reloading UITableView while Animating Scroll Calling  reloadData  on  UITableView  may not be the most efficient way to update your cells, but sometimes it’s easier to ensure the data you are storing is in sync with what your  UITableView  is showing. In iOS 10  reloadData  could be called at any time and it would not affect the scrolling UI of  UITableView . However, in iOS 11 calling  reloadData  while your  UITableView  is animating scrolling causes the  UITableView  to stop its scroll animation and not complete. We noticed this is only true for scroll animations triggered via one of the  UITableView  methods (such as  scrollToRow(at:at:animated:) ) and not for scroll animations caused by user interaction. This can be an issue when server responses trigger a  reloadData  call since they can happen at any moment, possibly when scroll animation is occurring. Example of s...

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...

Xcode & Instruments: Measuring Launch time, CPU Usage, Memory Leaks, Energy Impact and Frame Rate

When you’re developing applications for modern mobile devices, it’s vital that you consider the performance footprint that it has on older devices and in less than ideal network conditions. Fortunately Apple provides several powerful tools that enable Engineers to measure, investigate and understand the different performance characteristics of an application running on an iOS device. Recently I spent some time with these tools working to better understand the performance characteristics of an eCommerce application and finding ways that we can optimise the experience for our users. We realised that applications that are increasingly performance intensive, consume excessive amounts of memory, drain battery life and feel uncomfortably slow are less likely to retain users. With the release of iOS 12.0 it’s easier than ever for users to find applications that are consuming the most of their device’s finite amount of resources. Users can now make informed decisions abou...