1500+ ready React Native mobile app screens

Affiliates
Blog

How do I create a custom modal in React Native that feels native?

Close-up of a smartphone with the ChatGPT app open, highlighting AI technology on a textured surface.
Photo by Airam Dato-on on Pexels

Create a Custom Modal in React Native

To create a custom modal in React Native that feels native, use the built-in Modal component and customize its presentation with animations and styles. Here’s a straightforward example:

import React, { useState } from 'react';
import { Modal, View, Text, Button, StyleSheet, Animated } from 'react-native';

const CustomModal = () => {
    const [visible, setVisible] = useState(false);
    const fadeAnim = new Animated.Value(0);  // Initial opacity.

    const showModal = () => {
        setVisible(true);
        Animated.timing(fadeAnim, {
            toValue: 1,
            duration: 300,
            useNativeDriver: true,
        }).start();
    };  

    const hideModal = () => {
        Animated.timing(fadeAnim, {
            toValue: 0,
            duration: 300,
            useNativeDriver: true,
        }).start(() => setVisible(false));
    };

    return (
        <View style={styles.container}>
            <Button title="Show Modal" onPress={showModal} />
            <Modal
                transparent={true}
                visible={visible}
                animationType="none"
            >
                <Animated.View style={[styles.modalBackground, { opacity: fadeAnim }]}/>
                <View style={styles.modalContainer}>
                    <Text>This is a Custom Modal</Text>
                    <Button title="Close" onPress={hideModal} />
                </View>
            </Modal>
        </View>
    );
};

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    },
    modalContainer: {
        width: 300,
        height: 200,
        backgroundColor: 'white',
        borderRadius: 10,
        justifyContent: 'center',
        alignItems: 'center',
        shadowColor: '#000',
        shadowOpacity: 0.25,
        shadowOffset: { width: 0, height: 2 },
        shadowRadius: 4,
        elevation: 5,
    },
    modalBackground: {
        flex: 1,
        backgroundColor: 'rgba(0, 0, 0, 0.5)',
    },
});

export default CustomModal;

This code showcases how to create a custom modal with animated fade-in and fade-out effects. Adjust the dimensions and styles to match your app's design.

How do I use the React Native Modal?

The Modal component in React Native is a core component used to present content above an enclosing view. To use it, you just need to set the visible prop to toggle its visibility.

What libraries are helpful for creating modals?

Using libraries like react-native-modal (v12.0.0) can enhance modal functionality, offering features such as swipe gestures and customizable animations. Here’s a quick usage example:

import React from 'react';
import { View, Button } from 'react-native';
import { Modal } from 'react-native-modal';

const ExampleModal = ({ isVisible, onClose }) => {
    return (
        <Modal isVisible={isVisible} onBackdropPress={onClose}>
            <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
                <Text>Hello from the modal!</Text>
                <Button title="Close" onPress={onClose} />
            </View>
        </Modal>
    );
};

How can I add animation to my modal?

To integrate animations, React Native provides the Animated API. You can animate the modal's opacity or position when it renders. The previous examples demonstrate how to fade in and out using Animated.timing(), but you can also explore slide-in animations by modifying the transform property.

Why does the modal behavior differ between iOS and Android?

Modals in React Native can behave subtly differently across platforms. For instance, modals on iOS typically cover a larger area of the screen, while Android modals have a slightly different backdrop and may behave differently on older devices.

For better performance on mid-range Android devices, ensure your modal is lightweight, and avoid using heavy layouts or complex nested structures.

How do I pass data to a modal?

You can pass data to a modal by utilizing props. For example:

<ExampleModal isVisible={isVisible} onClose={handleClose} />

Inside the modal, access the data through props. If you need to display dynamic values, you can set them during the modal's invocation.

What should I do if my modal doesn't appear on the screen?

If your modal fails to render, check the following:

  • Ensure the visible state is correctly updated.
  • Confirm the Modal component is properly imported from React Native.
  • Verify that no parent components are preventing your modal from displaying properly.

If it’s still not showing, consider wrapping it in a View of adequate height and width, or check your layout's styles.

How can I customize the look of my modal?

Customize your modal with styles applied directly to the modal container. For example:

const styles = StyleSheet.create({
    modalContainer: {
        borderRadius: 20,
        padding: 20,
        backgroundColor: 'white',
    },
});

You can adjust the radius, padding, and background to reflect a more mobile-native appearance.

To ensure a seamless integration, test your modal across different devices to identify any performance issues or design inconsistencies.

Next steps would include exploring advanced modal functionalities such as gesture handling or implementing a backdrop component for enhanced user experience. You might also want to check out Clevens for beautiful, fully functional modal components to speed up your React Native development.