Introduction
React Native Alert is an API that displays an alert dialogue with a title and message that you specify. An alert dialog is triggered using the alert() method. This API can display static alerts and works on both android and iOS. Alert features only one positive (OK) button by default.
In iOS, users will receive an alert asking them to submit some information, and you can specify any number of buttons on iOS.
In android, This Alert dialogue has three separate lists of action buttons (neutral, negative, and positive). Any of these buttons will invoke the onPress callback method, which will dismiss the alert. To manipulate the alerts according to the way we want, we've different methods and type definitions.
Click on the following link to read further: Hooks in React JS
Methods of Alert
Alerts in react-native have two methods alert() and prompt() (iOS)
alert()
Syntax:
static alert (title, message?, buttons?, options?)
Parameters:
- title: It is of string type, it contains the main title of the dialog.
- message: It is of string type, it contains an optional message that appears below the dialog’s title.
- buttons: It is of type Button, an array containing alert buttons.
- options: It is of type Option, it is an optional alert configuration. Only available for android.
prompt()
Syntax:
static prompt(title, message?, callbackOrButtons?, type?, defaultValue?, keyboardType?)
Parameters:
- title: It is of string type, it contains the main title of the dialog.
- message: It is of string type, it contains an optional message that appears below the dialog’s title.
- callbackOrButton: It can be passed either as a button or as a function, it does the work of a button.
- type: It is of AlertType type and configures the text input.
- defaultValue: It is of string type, it contains the default text of input text.
- keyboardType: It is of string type, it contains the keyboard type of the first text field.
Apart from these methods, alert in react-native has several types of definitions also, you can find them on official documentation. We've had enough theories let's consider an example to get a good grasp on the alerts in react-native.