Modal & Alert
Learn to use Modal and Alert components for displaying overlays and user interactions in React Native applications. This is a foundational concept in cross-platform mobile development that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world React Native experience. Take your time with each section and practice the examples
Modal Component
Modal component provides a way to present content above an enclosing view. It's perfect for overlays, popups, and full-screen presentations.. This is an essential concept that every React Native developer must understand thoroughly. In professional development environments, getting this right can mean the difference between code that works reliably and code that breaks in production. The following sections break this down into clear, digestible pieces with practical examples you can try immediately
Modal Properties
- visible - Controls modal visibility — a critical concept in cross-platform mobile development that you will use frequently in real projects
- animationType - Animation style (slide, fade, none)
- transparent - Makes modal background transparent — a critical concept in cross-platform mobile development that you will use frequently in real projects
- onRequestClose - Callback when back button is pressed
- presentationStyle - How modal is presented — a critical concept in cross-platform mobile development that you will use frequently in real projects
- onShow - Callback when modal is shown — a critical concept in cross-platform mobile development that you will use frequently in real projects
Alert Component
Alert displays an alert dialog with a title, message, and buttons. It's useful for confirming actions or displaying important information.. This is an essential concept that every React Native developer must understand thoroughly. In professional development environments, getting this right can mean the difference between code that works reliably and code that breaks in production. The following sections break this down into clear, digestible pieces with practical examples you can try immediately
Modal & Alert Example
import React, { useState } from 'react';
import {
View,
Text,
TouchableOpacity,
Modal,
StyleSheet,
Alert,
TextInput,
ScrollView
} from 'react-native';
const ModalAlertExample = () => {
const [modalVisible, setModalVisible] = useState(false);
const [formData, setFormData] = useState({
name: '',
email: '',
message: '',
});
const showAlert = () => {
Alert.alert(
'Confirmation',
'Are you sure you want to proceed?',
[
{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel',
},
{
text: 'OK',
onPress: () => console.log('OK Pressed'),
},
],
{ cancelable: false }
);
};
const showPrompt = () => {
Alert.prompt(
'Enter Name',
'Please enter your name:',
[
{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel',
},
{
text: 'OK',
onPress: (name) => {
console.log('Name entered:', name);
setFormData(prev => ({ ...prev, name: name || '' }));
},
},
],
'plain-text',
'John Doe'
);
};
const showActionSheet = () => {
Alert.alert(
'Choose Option',
'Select an option from the list:',
[
{
text: 'Option 1',
onPress: () => console.log('Option 1 selected'),
},
{
text: 'Option 2',
onPress: () => console.log('Option 2 selected'),
},
{
text: 'Option 3',
onPress: () => console.log('Option 3 selected'),
},
{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel',
},
],
{ cancelable: true }
);
};
const handleSubmit = () => {
if (!formData.name || !formData.email) {
Alert.alert('Error', 'Please fill in all required fields');
return;
}
Alert.alert(
'Success',
'Form submitted successfully!',
[
{
text: 'OK',
onPress: () => {
setModalVisible(false);
setFormData({ name: '', email: '', message: '' });
},
},
]
);
};
return (
<ScrollView style={styles.container}>
<Text style={styles.title}>Modal & Alert Examples</Text>
<View style={styles.buttonContainer}>
<TouchableOpacity style={styles.button} onPress={showAlert}>
<Text style={styles.buttonText}>Show Alert</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={showPrompt}>
<Text style={styles.buttonText}>Show Prompt</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={showActionSheet}>
<Text style={styles.buttonText}>Show Action Sheet</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.button, styles.modalButton]}
onPress={() => setModalVisible(true)}
>
<Text style={styles.buttonText}>Open Modal</Text>
</TouchableOpacity>
</View>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => setModalVisible(false)}
>
<View style={styles.modalOverlay}>
<View style={styles.modalContent}>
<Text style={styles.modalTitle}>Contact Form</Text>
<TextInput
style={styles.input}
placeholder="Name"
value={formData.name}
onChangeText={(text) => setFormData(prev => ({ ...prev, name: text }))}
/>
<TextInput
style={styles.input}
placeholder="Email"
value={formData.email}
onChangeText={(text) => setFormData(prev => ({ ...prev, email: text }))}
keyboardType="email-address"
/>
<TextInput
style={[styles.input, styles.textArea]}
placeholder="Message"
value={formData.message}
onChangeText={(text) => setFormData(prev => ({ ...prev, message: text }))}
multiline
numberOfLines={4}
/>
<View style={styles.modalButtons}>
<TouchableOpacity
style={[styles.modalButton, styles.cancelButton]}
onPress={() => setModalVisible(false)}
>
<Text style={styles.modalButtonText}>Cancel</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.modalButton, styles.submitButton]}
onPress={handleSubmit}
>
<Text style={styles.modalButtonText}>Submit</Text>
</TouchableOpacity>
</View>
</View>
</View>
</Modal>
</ScrollView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 20,
backgroundColor: '#f5f5f5',
},
title: {
fontSize: 24,
fontWeight: 'bold',
textAlign: 'center',
marginBottom: 30,
color: '#333',
},
buttonContainer: {
gap: 15,
},
button: {
backgroundColor: '#2196F3',
padding: 15,
borderRadius: 8,
alignItems: 'center',
},
modalButton: {
backgroundColor: '#4CAF50',
},
buttonText: {
color: 'white',
fontSize: 16,
fontWeight: 'bold',
},
modalOverlay: {
flex: 1,
backgroundColor: 'rgba(0, 0, 0, 0.5)',
justifyContent: 'center',
alignItems: 'center',
},
modalContent: {
backgroundColor: 'white',
padding: 20,
borderRadius: 10,
width: '90%',
maxHeight: '80%',
},
modalTitle: {
fontSize: 20,
fontWeight: 'bold',
textAlign: 'center',
marginBottom: 20,
color: '#333',
},
input: {
borderWidth: 1,
borderColor: '#ddd',
borderRadius: 8,
padding: 12,
marginBottom: 15,
fontSize: 16,
},
textArea: {
height: 100,
textAlignVertical: 'top',
},
modalButtons: {
flexDirection: 'row',
justifyContent: 'space-between',
marginTop: 20,
},
cancelButton: {
backgroundColor: '#f44336',
flex: 1,
marginRight: 10,
},
submitButton: {
backgroundColor: '#4CAF50',
flex: 1,
marginLeft: 10,
},
modalButtonText: {
color: 'white',
fontSize: 16,
fontWeight: 'bold',
textAlign: 'center',
},
});
export default ModalAlertExample;