An alert dialog can be created using <Alert>
component in React Native. It can show a title, message and buttons. Title is required and everything else is optional.
The format of alert looks like this –
import {Alert} from 'react-native' Alert.alert(title, message, buttons, options) /** * Example Usage - Alert.alert( "Alert Title", "My Alert Msg", [ { text: "Cancel", onPress: () => console.log("Cancel Pressed"), style: "cancel" }, { text: "OK", onPress: () => console.log("OK Pressed") } ], { cancelable: true, onDismiss: () => console.log("Cancelled from outside") } ); */
It works in both Android and iOS but there are few distinctions among the Alert dialogs of Android and iOS –
- On Android we can have at most 3 buttons in Alert dialog but there is no limit on iOS.
- We can dismiss the dialog by clicking outside it in Android, but not in iOS.
- iOS provides functionality to show input fields in Alert dialog to capture data from user. No such feature is provided in Android.
Code Example
import React, { useState } from "react"; import { View, StyleSheet, Button, Alert } from "react-native"; const App = () => { const createBasicAlert = () => Alert.alert( "Alert Title", ); const createOneButtonAlert = () => Alert.alert( "Alert Title", "This alert has 1 button", [ { text: "OK", onPress: () => console.log("OK Pressed") } ] ); const createTwoButtonAlert = () => Alert.alert( "Alert Title", "This alert has 2 buttons", [ { text: "Cancel", onPress: () => console.log("Cancel Pressed"), style: "cancel" }, { text: "OK", onPress: () => console.log("OK Pressed") } ] ); const createThreeButtonAlert = () => Alert.alert( "Alert Title", "This alert has 3 buttons", [ { text: "Ask me later", onPress: () => console.log("Ask me later pressed") }, { text: "Cancel", onPress: () => console.log("Cancel Pressed"), style: "cancel" }, { text: "OK", onPress: () => console.log("OK Pressed") } ] ); return ( <View style={styles.container}> <Button title={"Only Title Alert"} onPress={createBasicAlert} /> <Button title={"1-Button Alert"} onPress={createOneButtonAlert} /> <Button title={"2-Button Alert"} onPress={createTwoButtonAlert} /> <Button title={"3-Button Alert"} onPress={createThreeButtonAlert} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: "space-around", alignItems: "center" } }); export default App;
Output –
This above image shows the Alert dialogs in iOS phones. We have 4 phones and each one is showing a different Alert style –
- 1st phone is showing an Alert dialog with title only. The OK button is also there but that is added by system itself.
- 2nd phone is showing an alert with title, message and single button.
- 3rd phone is showing an alert with 2 buttons.
- 4th phone is showing an alert with 3 buttons.
This is the image of Alert dialogs on Android phones. Similar to iOS image, we have 4 different Android phones here and each one is showing alerts differently.
In the code, we have created 4 functions to call alert dialog. Since alert is a static function of Alert class, so we can call it separately from other JSX.
Live Demo
Conclusion
Alert dialogs are the critical components of an application. They are not specific to mobile apps, else they are being used from the very beginning of web development. In React Native, we can call it using Alert.alert()
function. It accepts title, message, buttons and some optional options.
React Native Series
Alert
- Basic Alert
- Dismiss on Clicking Outside
- Input Fields in Alert Dialog – Prompt()
- Dark-Light Theme of Dialog
ActivityIndicator
- Basic Circular Loader
- Change size of Circular Loader
- Show/hide Circular Loader
- Change color of Circular Loader
Button
FlatList
- Simple List
- Single Item Selection from List
- Multiple Item Selection from List
- Adding separator between list items
- Multiple columns List
- Showing Message in Empty List
- Add Footer to the List
- Add Header to the List
- Horizontal List
- Inverted List
- Pull to Refresh in List
- Infinite Loading List
SectionList
ScrollView
- ScrollView
- Stick Single Item at Header
- Stick Multiple Items at Header
- Stick Item at Footer
- Hide Sticky Element on Scroll
Image
- Display Image from remote url
- Display local storage image
- Display Base64 Image
- Display Gif & Webp Images
- Adding Blur to Image
- Displaying loader for Image
- Resizemode for Images
- Setting Default Placeholder Image
- Background Image
Modal
RefreshControl
- RefreshControl
- Change Refresh Loader Color
- Change Refresh Loader Size
- Change Refresh Loader Background Color
- Title under Refresh Loader
- Change color of title under refresh loader
StatusBar
- Get StatusBar Size
- Change StatusBar Background Color
- Display StatusBar icons & text in While Color
- Display StatusBar icons & text in Dark Color
- Hiding StatusBar
- Translucent StatusBar
Switch
Text
- Adding Text
- Bold Text
- Italic Text
- Underline Text
- Selecting Text for copy-paste
- Changing Highlight Color of Text Selection
- Fit text in View box
- Clickable anchors in text
- Truncate Lengthy Text
TextInput
- Simple Input Field
- Auto Capitalize Text in Input Field
- Multiline Input Field
- Hide Cursor in Input Field
- Clear input Field using X
- Clear input Field when focused
- Change Cursor Color in Input Field
- Disable input field
- Icon at the left of Input Field
- Dark-Light Keyboard
- Avoid Overlapping of Keyboard
- Limiting Characters in Input Field
- Numeric Keyboard
- Email Id Keyboard
- Phone number Keyboard
- Url Keyboard
- Placeholder in input field
- Placeholder Color in Input Field
- Password Input Field
- Programmatically select text in Input Field
- Change Text Selection Color in Input Field
- Select Whole text in Input Field on Focus
- Write text from center in input field
- Changing underline color of input field
TouchableWithoutFeedback
TouchableHighlight
TouchableOpacity
Pressable
Appearance
AppState
ToastAndroid
Dimensions
Keyboard