In this tutorial I show how to manage navigation and application state by building a simple app with react-navigation and redux.
Prerequisite: you should already be familiar with React Native, React Navigation, and Redux. If you’re just getting started with React Native, I highly recommend this course:
We’re going to build an app made of two pages:
By building this app, you will learn:
With this knowledge, you’ll be able to build more complex apps.
Note: For the remainder of this tutorial, I will use the terms ‘page’ and ‘screen’ to mean the same thing.
We’re going to build this app with the Expo XDE.
You can download Expo for your OS from the Expo XDE GitHub page.
Then head over to the installation instructions on the Expo Docs. These will show you how to install the XDE on your desktop, and run apps within Expo on simulator/device.
As we’ll be running the app on the simulator, you’ll also need to download Xcode or Android Studio.
Upon launching Expo, this page is presented:
The project will be created, then the React Native packager will start.
To run the app in the simulator, select Device -> Open in iOS Simulator.
Once the simulator is booted up, the following screen appears:
As the project is now created, it can be opened with an editor of choice. I use Visual Studio Code with the React Native Tools extension.
Before we can code our app, we need to install all the dependencies it needs.
Open a terminal, move to the project folder you created in Expo, and type:
npm install --save react-navigation redux react-redux
npm install
Then, make sure to hit the Restart button in Expo. If you don’t do this, the new dependencies won’t be recognized and the simulator will throw a red error screen if you try to use them.
Time to build our app. I organized my project folders like this:
/src
/actions
ColorChangedAction.js
/components
AppNavigator.js
ChooseColorPage.js
MainPage.js
/reducers
AppReducer.js
ColorReducer.js
NavReducer.js
/state
Colors.js
Copy-paste the following code into the Colors.js file:
<a href="https://medium.com/media/763d0e84353134bea1b88e619dfb02df/href">https://medium.com/media/763d0e84353134bea1b88e619dfb02df/href</a>
Next, create the MainPage with a default background color and a button:
<a href="https://medium.com/media/c5c96dee26ada83ffca1d2e5eb58b3fa/href">https://medium.com/media/c5c96dee26ada83ffca1d2e5eb58b3fa/href</a>
A few notes:
Time to hook up our MainPage into our root App.js file. Replace the old contents with this:
<a href="https://medium.com/media/601d89b015c895d9573e0d22e76555eb/href">https://medium.com/media/601d89b015c895d9573e0d22e76555eb/href</a>
Refreshing the simulator yields this:
Not pretty, but it shows the background color and our button as intended.
If needed, here is a snapshot of what we’ve built so far: GitHub Code Snapshot 1.
We are now ready to add some navigation to our app.
To do this, open the AppNavigator.js file and add these contents:
<a href="https://medium.com/media/8673ea2e651dc48b1952be89296de790/href">https://medium.com/media/8673ea2e651dc48b1952be89296de790/href</a>
This code is borrowed from the Redux Example in the react-navigation project.
It defines a StackNavigator, using our MainPage as its main screen.
It also sets up AppWithNavigationState, a top-level container holding the navigation state. If this seems unclear, don’t worry. This is standard boilerplate code in React Navigation and we’ll just use it for now to get things going.
Time to write the navigation reducer, which will hold the navigation state inside the Redux store. Open the NavReducer.js file and add the following:
<a href="https://medium.com/media/28cab0b95ad06ee61cd972165b9f2246/href">https://medium.com/media/28cab0b95ad06ee61cd972165b9f2246/href</a>
This reducer defines the initial navigation state of our app. Again, boilerplate code.
Now, let’s open the AppReducer.js file and add this:
<a href="https://medium.com/media/51e7b1b34a6a9efaaa18ee0b25d50b57/href">https://medium.com/media/51e7b1b34a6a9efaaa18ee0b25d50b57/href</a>
As our application grows, we may need other reducers alongside our NavReducer. So we can combine them all together inside AppReducer.
Finally, we’re able to update our App.js to use all these new goodies:
<a href="https://medium.com/media/6187cf29991fa67a6d7985f00b98840a/href">https://medium.com/media/6187cf29991fa67a6d7985f00b98840a/href</a>
The render method returns a provider with the created redux store, and holds our top-level component. Again, this is just boilerplate code needed to hook things up with Redux.
If we refresh the simulator, we now see a navigation bar appearing on top:
After all this code, you may get some errors on your simulator if anything is missing. If so, use this code snapshot to get back on track: GitHub Code Snapshot 2.
Now that we have a MainPage inside a StackNavigator, we’re ready to add the ChooseColorPage so we can navigate to it.
Open the ChooseColorPage.js file and add the following code:
<a href="https://medium.com/media/71277ec8de4cff03ca5a0b5f340f9aa6/href">https://medium.com/media/71277ec8de4cff03ca5a0b5f340f9aa6/href</a>
A few notes:
Next, we have to make our AppNavigator aware of the new ChooseColorPage screen. Let’s update it in the AppNavigator.js file:
<a href="https://medium.com/media/b1835506e75460259df37a83b56b1b65/href">https://medium.com/media/b1835506e75460259df37a83b56b1b65/href</a>
Finally, add the code to navigate to the ChooseColorPage when the Choose Color button is tapped on the MainPage.
<a href="https://medium.com/media/6234e2e5883d006bec6f6806e0390fa6/href">https://medium.com/media/6234e2e5883d006bec6f6806e0390fa6/href</a>
If we refresh the simulator now and tap on Choose Color, the app navigates to the new screen, which shows three buttons:
Note: Calling navigation.navigate('ChooseColor') works because we have named ChooseColor as one of the routes in our AppNavigator.
Tapping on the back button or on any of the color buttons brings us back to the main page, but the background color doesn’t change according to our selection.
Let’s fix that in the next section.
Again, if something is not working, you can get my saved code snapshot to this point: GitHub Code Snapshot 3.
We’ll use redux to set the background color of our MainPage as our application state.
To do this, we need to define a Color Changed action, and a Color Reducer.
Open the ColorChangedAction.js file and add the following:
<a href="https://medium.com/media/1e61e790ffc15c3b299105d458c95fa8/href">https://medium.com/media/1e61e790ffc15c3b299105d458c95fa8/href</a>
Then, open ColorReducer.js add add this:
<a href="https://medium.com/media/5fe3ea16aa37626fd77345026cd2e2e4/href">https://medium.com/media/5fe3ea16aa37626fd77345026cd2e2e4/href</a>
In order for this reducer to be used, we need to add it to the AppReducer.js like so:
<a href="https://medium.com/media/21ca7f53b6fb2fb240adaf3f4edd7570/href">https://medium.com/media/21ca7f53b6fb2fb240adaf3f4edd7570/href</a>
Now, we’re ready to call our colorChanged action when the user selects a color in the ChooseColorPage. This is the updated ChooseColorPage.js file:
<a href="https://medium.com/media/6a4bdd53009556260baef4f6889387fa/href">https://medium.com/media/6a4bdd53009556260baef4f6889387fa/href</a>
Note that we have made three changes:
At this stage, we can refresh the simulator and run. If we choose a different color, the background color of the MainPage still doesn’t change. 🤔
This is because we haven’t told MainPage to use the new state.
Easy to fix. Open MainPage.js and add the required code:
<a href="https://medium.com/media/0fbae240657db52c7903e7c08c305e67/href">https://medium.com/media/0fbae240657db52c7903e7c08c305e67/href</a>
A few notes:
If we try the app again in the simulator, we are now able to change the background color. 😎
Updated snapshot: GitHub Code Snapshot 4.
When we tap the Choose Color button in the MainPage, the ChooseColorPage slides in from the right. This is the default navigation animation inside StackNavigator.
What if we wanted to present the ChooseColorPage modally instead?
This is easily done by changing the configuration of our AppNavigator like so:
<a href="https://medium.com/media/60f6e613162ec0677c6513586beddf17/href">https://medium.com/media/60f6e613162ec0677c6513586beddf17/href</a>
Note the addition of navigationOptions with a headerLeft: null property inside ChooseColor, and the mode: ‘modal’ parameter.
If we try this on the simulator, the ChooseColorPage now slides in from the bottom.
React Navigation is very customisable. I recommend spending some time reading the documentation for the project, to learn all the things you can do with it.
We have learned how to:
You can find the complete source code on GitHub here.
I also shared the project publicly on Expo here.
I hope you enjoyed this tutorial. A good next step from here is to look at the official React Navigation Examples, as well as the other tutorials from the community.
Comments and feedback are appreciated. 😊
And if you Clap Clap Clap, I may even do a step-by-step video tutorial. 😉