← Back to Chapters

Custom Modal – React Native

? Custom Modal – React Native

? Quick Overview

A custom modal in React Native allows developers to design fully customized dialog boxes instead of relying only on the default Modal component behavior.

? Key Concepts

  • Modal visibility using state
  • Overlay background
  • Centered animated content
  • Reusable component structure

? Syntax / Theory

React Native modals are typically controlled using a boolean state. Custom modals are created using views layered above the screen with absolute positioning and optional animations.

? Code Example

? View Code Example
// Custom modal component using React Native
import React, { useState } from "react";
import { View, Text, TouchableOpacity, StyleSheet } from "react-native";

export default function App() {
  const [visible, setVisible] = useState(false);

  return (
    <View style={styles.container}>
      <TouchableOpacity onPress={() => setVisible(true)}>
        <Text style={styles.openBtn}>Open Modal</Text>
      </TouchableOpacity>

      {visible && (
        <View style={styles.overlay}>
          <View style={styles.modalBox}>
            <Text style={styles.title}>Custom Modal</Text>
            <TouchableOpacity onPress={() => setVisible(false)}>
              <Text style={styles.closeBtn}>Close</Text>
            </TouchableOpacity>
          </View>
        </View>
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  container:{ flex:1, justifyContent:"center", alignItems:"center" },
  overlay:{ position:"absolute", top:0, left:0, right:0, bottom:0, backgroundColor:"rgba(0,0,0,0.5)", justifyContent:"center", alignItems:"center" },
  modalBox:{ width:250, padding:20, backgroundColor:"#fff", borderRadius:10 },
  openBtn:{ color:"blue" },
  closeBtn:{ color:"red", marginTop:10 },
  title:{ fontSize:18, fontWeight:"bold" }
});

? Live Output / Explanation

When the button is pressed, the modal overlay appears centered on the screen. Closing the modal updates the state and hides the overlay.

? Interactive Example

Click the button inside the phone simulator below to see how the logic works in real-time.

? Use Cases

  • Confirmation dialogs
  • Login / Signup popups
  • Alerts and warnings
  • Custom forms

? Tips & Best Practices

  • Always block background interaction
  • Use animations for better UX
  • Keep modal components reusable

? Try It Yourself

  • Add slide or fade animation
  • Make modal reusable with props
  • Add backdrop press to close