Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Use the React Native Modal Component with React Navigation

    Spencer CarliSpencer Carli
    react-nativeReact Native

    In this lesson you will learn how to display and access the React Native Modal component from any of your navigator’s screens and ensure it overlays the entire app. We will take a look at screenProps prop made available to React Navigation navigators and how to use it.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: To begin, we're rendering two screens, a Home and Details screen. Each one of these has a button in which we can go to the Details screen and we can open up the Modal from each screen once it's set up. These are being rendered via StackNavigator coming from React Navigation, and I'll just set some basic navigation options of Title.

    Now if we want to use the core React Navigation and Component of Modal to render a Modal, what we're going to do is set up a new class of app which will extend React.Component. Inside of here, we're going to set some initial state of modalVisible set to false.

    We're then going to create a new method called ChangeModalVisibility. We're going to give that one argument of modalVisible, and I'll set the default value for that to false. Then inside of this method, we'll say this.setState, and I'll change modalVisible.

    Next, we'll go ahead and set up our render function, and before we do that we're going to import a few things from React Native. We'll import the View, Modal, Style Sheet, and Text. Before we go writing anymore code, I'm just going to paste in a few pieces of styling information.

    We've got a Modal container which is just going to allow us to take up the entirety of the screen that the Modal's rendered in, and then a Modal body which will actually surround the content that we want to render inside of our Modal.

    Now inside of our render, we'll set up our Return, and we're going to have a containing View with a style equal to Flex One, just to ensure it takes up the entirety of the space it's given. We're then going to render our main apps Stack and replace our export default with just app.

    With everything rendering in the same way, we can go ahead and start rendering our Modal and to do that we'll set up our Modal component. Inside of there, we'll set a SafeAreaView.

    Inside of our SafeAreaView, we're going to render a View with a style of styles.modalBody. We'll render some text, and we'll render a button. In this button, we're going to call the this.ChangeModalVisibility function, and we'll pass an argument of false to close the Modal.

    Before we move on, we need to set the style for our SafeAreaView, and we're going to set that to styles.modalContainer. We're also going to pass a few props to our Modal component, the first of which is going to be visible. That's going to be equal to this.state.modalVisible.

    We're also going to specify the animation type that should be used. In this case, we're going to use the animation of fade. We can now go ahead and, using screenProps, pass our function of ChangeModalVisibility down to any children components and screens.

    Using ChangeModalVisibility and looking inside of our screens, what we can do is actually access our screenProps as another prop inside of any component that has been registered with the navigator.

    Using screenProps, we can then go ahead and replace our onPress and say screenProps.ChangeModalVisibility. In this case, we want to pass an argument of true. We'll do the same for both the Home screen and the Details screen.

    Now when we press Go To Modal, you can see a Modal fade in, and when we press Close Modal, it will fade out. We can do the exact same thing from our Details screen once we import our screenProps prop.

    Now when we go to Details, we can open our Modal, close our Modal, and do the exact same thing on the Home screen.