Building a React Native App with Complex Navigation in 2024
Do you want to build a React Native app with multiple screens and a complicated navigation journey?
In this tutorial you will create a React Native app with layered screens, nest drawers with other navigators, and handle platform-specific navigation. You will learn the advanced concepts you might need to use when creating a professional app for production.
Here is a diagram showing you what you're going to build.
In this diagram, solid lines represent navigators (tabs, drawer, and stack) and dotted lines represent the different screens.
Your root navigator will be a stack navigator. It manages the splash screen during app loading. It includes the main and authentication screens. It also contains a web-only "Not Found" screen.
The authentication screens will include a screen to log in, a screen to reset the password, and a screen to register.
After logging in, you will have access to the main screens, which are the home screen, the options screen, the details screen, and the settings screen.
On Android, you usually access the settings through a burger menu, while on iOS, it is more common to have a tabs layout. You will use the respective platform’s specific design dynamically. Additionally, within the home stack you will display the options screen using a modal transition.
The app will feature 8 screens, including the splash screen. It uses a standard layout with a list of items on the home screen. You can filter these items using the options screen. You can view specific items on the details screen. Note: This tutorial will not include adding the list interface.
2. Create the basic project
If you want to code along, this section will guide you step by step through setting up the project. (The bottom of this article contains a trouble shooting section handling common errors you might encounter when running Expo for the first time.)
Create a new project.
Give it a proper name.
Install the dependencies for the drawer.
Start the simulator.
If you want to add the Expo Router to an existing app, check out the docs.
Loading Screen
Delete all files in app/ except for app/(tabs)/, app/_layout.tsx, app/+html.tsx and app/+not-found.tsx and rename the app/(tabs)/ folder to app/(main)/.
Change the content of your root layout in app/_layout.tsx to the following.
React Native Elements
To make the screens look decent, you can use React Native Elements. Install the React Native Elements package.
Add the ThemeProvider from React Native Elements to your root layout.
Authentication Screens
Add a stack above the "(main)" stack in the root layout.
Create a layout for the authentication screens at app/(login)/_layout.tsx.
And adjacent to that file another screen for the app/(login)/forgot-password.tsx flow.
Next, create the layout for the authentication tabs in app/(login)/(auth)/_layout.tsx.
Create the login screen in app/(login)/(auth)/index.tsx.
Create the register screen in app/(login)/(auth)/register.tsx:
Main Screens
Now, you're going to create the main screens.
Delete all files in your app/(main)/ folder because you're going to start from scratch.
The main layout will handle the switching between different navigators based on the platform. You can use React Native's Platform module to detect where your app is running.
Create the main layout in app/(main)/_layout.tsx.
Notice how you hide the header of the drawer based on whether the user's current path. If they're on the home route, hide the header. Otherwise, both the header and the stack navigator of the home would render a header.
Now create the settings screen at app/(main)/settings.tsx.
Next create the layout for the home stack at app/(main)/(home)/_layout.tsx.
You configure the options screen to show as a modal in that screen's options prop.
Create the home screen at app/(main)/(home)/index.tsx.
Create the details screen at app/(main)/(home)/details.tsx.
Lastly, create the options screen at app/(main)/(home)/options.tsx.
Now you're done. This how your app should look like.
Redirecting Authenticated Users
There is one more thing. This tutorial contained a short cut by redirecting your user manually when logging out. Usually, those buttons should invalidate your users session (e.g. remove the credentials from your storage solution).
If you want to authenticate users and redirect them based on their authentication status, you should use Expo's Redirect component in your root layout.
Bonus: Troubleshooting
On Mac, you might get an error trying to start the iOS simulator.
Error: xcrun simctl boot FE32B4BF-3BE2-44AD-A839-5E8602C4853E exited with non-zero code: 2
An error was encountered processing the command (domain=NSPOSIXErrorDomain, code=2):
Unable to boot device because we cannot determine the runtime bundle.
No such file or directory
Here is how you fix it. Open Xcode, go to Settings > Platforms and then install the iOS platform.
Open the simulator and start it.
Useful Expo commands:
Learn senior fullstack secrets
Subscribe to my newsletter for weekly updates on new videos, articles, and courses. You'll also get exclusive bonus content and discounts.