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.


    Open a Modal from Tab Bar in React Navigation

    Spencer CarliSpencer Carli
    react-nativeReact Native

    In this lesson you will learn how to use the navigation option of tabBarOnPress to override the functionality of a tab press in a React Navigation TabNavigator.



    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: We're starting off with an application that's using a tab navigator to render two screens -- one being Favorite, one being Recents.

    On our Favorite screen, we can go ahead and open a Modal which again is a very basic component. This time it's being rendered via a StackNavigator of a mode of Modal which is allowing it to come up from the bottom of the screen.

    Now what we want to do is replace this button which is allowing us to open the Modal and be able to open it via the tab, or something similar to when you're adding a new picture to Instagram. To do this, what we're going to do is first go up to our Imports from React Native and import a View.

    We're then going to go to our tab navigator. We want this to show up between our Favorites and our Recents, so we'll go between those and add it in here. We want to specify a key for this screen which is going to be New. We're going to specify it a screen that should be rendered, and we're just going to render a basic view because you're not actually going to see the screen at any point.

    We're then going to specify some navigation options. Using our navigation options, we can actually specify a function and from this function we want to return an object.

    We'll specify our title that should be used in the navigation bar which we'll set to New. We're also going to specify a tab bar icon and this tab bar icon's coming from the Material Community Icons which is via Expo vector icons.

    Now what's really interesting is our tab bar OnPress which allows us to actually override what happens when this tab bar item is pressed. When this happens, what we're going to do is actually -- from our navigation options -- our first argument, we can go ahead and destructure it and access navigation just like we would on a normal screen.

    Now from within our tab bar OnPress, we can say navigation.navigate and we can specify the screen while it opened which is going to be Modal. Again, we've got access to our parent screens that are registered in parent navigators.

    Now when we save this, the application's going to re-render, and you can see that we've got this new item down here. We go to Favorites, it goes to Favorites. If we go to Recents, it goes to Recents. If we press New, it'll go ahead and open up that Modal from the bottom like we would expect it to, like it did when we had the button on our Favorites screen.