React Native Alert !!
6D-Shrinidhi Rao
1 min read
Alert launches an alert dialog with the specified title and message.
Optionally provide a list of buttons. Tapping any button will fire the respective onPress callback and dismiss the alert. By default, the only button will be an 'OK' button.
This is an API that works both on Android and iOS and can show static alerts. Alert that prompts the user to enter some information is available on iOS only.
import React from 'react';
import {View, StyleSheet, Button, Alert} from 'react-native';
const App = () => {
const createTwoButtonAlert = () =>
Alert.alert('Alert Title', 'My Alert Msg', [
{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel',
},
{text: 'OK', onPress: () => console.log('OK Pressed')},
]);
const createThreeButtonAlert = () =>
Alert.alert('Alert Title', 'My Alert Msg', [
{
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={'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;
This is in Expo Go Version But will Work in CLI Version too.
0
Subscribe to my newsletter
Read articles from 6D-Shrinidhi Rao directly inside your inbox. Subscribe to the newsletter, and don't miss out.
React Nativereact native app developmentReact Native app development servicesreact-navigation#react-native on hashnodeandroid with react native
Written by