React Native Alert

In this part of the tutorial, we will learn about react-native's alert API. It is compatible with both android and IOS.

What is API?

Answer - API stands for Application programming interface, API allows communication between two software, and it is also responsible for data transmission between two software.

Let us understand API with an example-

Here we can consider that API(Application programming interface) works as a waiter. Suppose you went to a hotel and you ordered some drinks! But did you make that drink by yourself? Obviously, no, because it was the waiter who took the order and also served the order. You do not know what happened in between.

React native Alert: It displays an alert with a particular message. The Alert () method is used to execute this process. Usually, it is a popup.

React native allows three types of alerts -

  • Simple Alert
  • Two options alert
  • Three options alert

Alert Buttons for IOS only -

    We can define as many buttons as we want. Each has a style that can be default .

Alert Button for android:-

we can only use three types of buttons on android

  • Positive button
  • Negative button
  • Neutral button

If there is only one button, it will be "OK."

If there are two buttons, it would be "Cancel"  and "OK".

If there is a three-button, then it would be "Ask Me Later", "Cancel,"  and "OK".

Let's work with Alert

Create one separate file, alert_ex.js.

Alert_ex.js code

import React from 'react.'
import {Text, TouchableOpacity, StyleSheet,Alert } from 'react-native'


const Alert_ex = () => {
   const your_Alert = () =>{
      Alert.alert(
         'this is an alert'
      )
   }
   return (
      <TouchableOpacity onPress = {your_Alert} style = {styles.button}>
         <Text style={{fontSize:20}}>Alert</Text>
      </TouchableOpacity>
   )
}
export default Alert_ex


const styles = StyleSheet.create ({
   button: {
      backgroundColor: '#cc00cc',
      alignItems: 'center',
      borderRadius:10,
       Width:100,
      marginTop: 300,
      justifyContent:'center',
     
   }
})

After updating the code of alert_ex.js, go to your App.js file. We will now export components from react_ex.js to app.js to give the final shape to our app and execute our plan. Make sure to follow the naming convention. If you are changing the name of the file, then you are expected to change the path of the app.js file. write your file name './yourfilename'

Otherwise, you will be facing a bug, and if you are changing the name of the component, then make sure to change the name of the imported file of App.js.

Open your App.js file, remove if any code is there and update the code as follows-

App.js code

import React from 'react.'
import Alert_ex from './alert_ex.js'


const App = () => {
   return (
     
      <Alert_ex />
   )


}


export default App

Output:

React native Alert
React native Alert

So now click on Alert. What do you see? It shows 'this is an alert', which is an alert. Let us understand the functioning of code.

Alert_ex.js explanation-

<TouchableOpacity onPress = {your_Alert} style = {styles.button}>

Here we have created one button and after pressing that button it  will call your_Alert and then we have styled it.

const Alert_ex = () => {
   const your_Alert = () =>{
      Alert.alert(
         'this is an alert'
      ) }

Here we have created one component named Alert_ex, and we have defined another component inside, which is your_alert, and then we have provided an alert inside it. So that is why when the button is pressed, "this is an alert" message is displayed".if you want any other message displayed, then You can edit it.

App.js explanation :

import Alert_ex from './alert_ex.js'
Here we have imported the Alert_ex component from the alert_ex file.


 return (
     <Alert_ex />
   )

Here we have used the component, and the working of this component is explained earlier in the Alert_ex.js file explanation.

The previous Alert has one drawback: we do not have any cancel button, so how will we get back to the home screen.We will now add the cancel feature to our previous project. let us see what will be the code

Alert_ex.js code:

import React from 'react'
import {Text, TouchableOpacity, StyleSheet,Alert } from 'react-native'


const Alert_ex = () => {
   const your_Alert = () =>{
     Alert.alert(
'Our Title for first alert',
[
{
text: 'Cancel',
onPress: () => console.log('End user is not at all interested for it'),
style: 'cancel',
},
{text: 'OK', onPress: () => console.log('End user is interested for it')},
] );


      
   };
   return (
      <TouchableOpacity onPress = {your_Alert} style = {styles.button}>
         <Text style={{fontSize:20}}>Alert</Text>
      </TouchableOpacity>
   )
}
export default Alert_ex


const styles = StyleSheet.create ({
   button: {
      backgroundColor: '#cc00cc',
      alignItems: 'center',
      borderRadius:10,
       Width:100,
      marginTop: 300,
      justifyContent:'center',
     
   }
})

App.js file will remain the same.

Output:

React native Alert

 Here we have added cancel and ok.

Explanation :

 const your_Alert = () =>{
     Alert.alert(
'Our Title for first alert',
[
{
text: 'Cancel',
onPress: () => console.log('End user is not at all interested for it'),
style: 'cancel',
},
{text: 'OK', onPress: () => console.log('End user is interested for it')},
] );

 If you want to add anything to your Alert, you must follow this structure. Text will represent the displayed text on Alert like cancel is written in text, and you can see cancel on the output screen same goes for OK. Cancel most probably means that the user is not interested that way

The console will be printing 'End-user is not at all interested in it'.


Related Topics

React Native Tutorial

Introduction to react native : React-native is an open-source javascript framework that can be used to develop native mobile apps for IOS and android. It is released by Facebook in 2015...

4 minutes read.

React Native Alert

In this part of the tutorial, we will learn about react-native's alert API. It is compatible with both android and IOS. What is API? Answer - API stands for Application programming interface,...

4 minutes read.

React Native Side Drawer

In this part of the react-native tutorial, we will be learning about the react-native side drawer. We have already studied the status bar and various parts of react-native navigation, including...

5 minutes read.

React Native Status Bar

What is the status bar? The simplest and easiest answer to this question can be answered by looking at your mobile phone's top section. What can you see? Most probably, it's...

4 minutes read.

Redux in React Native

 It is a really amazing library of javascript, react, and react-native. It is very popular in react native domain. Every react and react native developer must know the redux it...

7 minutes read.

React Native API

API API stands for an Application programming interface. API allows communication between two software and is also responsible for data sharing. It takes the asked data from the server and delivers...

6 minutes read.

TouchableOpacity in React Native

Touchable opacity is the most suited example if you want to make views respond to proper touch. If you want to control the opacity then it can be done by...

3 minutes read.

React Native Toast

Introduction: It is a message that will be displayed on the screen for a short span of time to interrupt the user. Primarily it is displayed by any user action....

5 minutes read.

React native calender

In this article, we will be learning to add calendar features to react native app. So before creating the calendar, we do need to install a few packages or add some...

3 minutes read.

Difference Between React.js and React Native

React js vs. React-native React native:  react-native is an open-source javascript framework that can be used to develop native mobile apps for IOS and android. Facebook released it in 2015 and witnessed...

3 minutes read.

React Native Fonts

The font is one of the important essences of any good UI(User Interface). It is very important to have a clear and beautiful font in your app. In this module,...

4 minutes read.

States in React Native

We can control data of components of react-native by state and props. We are familiar with props and used them multiple times previously. Props: This is used to customize components and...

3 minutes read.

React Native Pressable

It manages all the touch inputs, and it is one of the core wrappers introduced in July 2022. The Two important features of pressable are listed below- onPressInonPressOut onpressIn: We already know the...

3 minutes read.

React Native Creating Components

React elements are where all React codes reside. The React elements are very similar to the regular React components, which have a unique style and offer contrast. Working with views When you...

3 minutes read.

React Native indicator

React native activity indicator : React native activity indicator is one of the react-native components. We all have seen it several times. It's the loader. Look at the picture below - So now...

4 minutes read.

What is a Bridge in React Native?

A React Native is generally composed of two fields or two parts: The first part is the code of JavaScript.The second one is native code. The ability to build a bridge between...

3 minutes read.

React Native Animations

Animation: animation is a method in which puppets, figures, drawings, etc., appear as moving creatures, making an illusion that things are moving. It is a unique way of expressing something...

6 minutes read.

TouchableHighlight in React Native

It is just a wrapper for making touch work properly, and it is the conventional way of handling touch gestures. Today's most future-proof solution is pressable API. pressable manages all...

2 minutes read.

React Native Sound

It is a great way to connect to the user. Just a tiny "ting" of any email received, or some sound for deleting a file or sound for sending a...

8 minutes read.

React Native Components

React Native components are the same as ordinary react components, but they are different in terms of rendering and styling. All react code lives in react components. They are independent...

16 minutes read.