Switch from One Screen to another using React Navigation 3.+ (Router)

This is an Example to Switch from One Screen to another using React Navigation 3.+ (Router). We will use react-navigation library for the Navigation.

React Native Navigation (Version 3+) is used to switch from one screen to another in the desired manner. This navigation solution is written entirely in JavaScript (so you can read and understand all of the sources), on top of powerful native primitives.

This is the updated post for the React Navigation Version 3+ . According to the official release “it now depends on react-native-gesture-handler. This library provides an excellent set of primitives for leveraging the operating systems’ native gesture APIs and has enabled us to fix a variety of issues with stack and drawer navigators.”

Navigate using

In this example, we will navigate from the first screen to second on a click of a button. So Let’s get Started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To use createStackNavigator you need to install react-navigation and react-native-gesture-handler dependencies.

To install these dependencies open the terminal and jump into your project

Run the following commands

This command will copy all the dependencies into your node_module directory. –save is optional, it is just to update dependencies in your package.json file.

Linking of Dependencies

After installing the dependencies you need to link it with your project using

After linking the dependency if you face  Could not Compile Settings File project/android/settings.gradle error while building and installing the project then you can find the solution here.

Project Structure

To start with this Example you need to create a directory named pages in your project and create two files FirstPage.js and SecondPage.js

Now Open App.js in any code editor and replace the code with the following code.

Change in this example as compared to our previous example on React Navigation (Version 2+) is:

In Version 2+ you just need to export the default class.

But In Version 3+ you have to wrap it with the createAppContainer and then you have to export it.

For this, you have to import createAppContainer from ‘react-navigation’. Have a look at the updated code. We are using “react-navigation”: “3.0.8” for this example.

App.js

Open FirstPage.js in any code editor and replace the code with the following code.

FirstPage.js

Open SecondPage.js in any code editor and replace the code with the following code.

SecondPage.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

IOS

     

Android

     

That was the React Native Navigation Version 3.+. We are also working to list out the other changes in Version 3+ so keep visiting or subscribe us.

If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Example of Popup Dialog in React Native

Here is an Example of Popup Dialog in React Native. We will use PopupDialog component provided by react-native-popup-dialog to make a Popup Dialog in React Native.

We can use Alert instead of the popup dialog if we just want to show the text but when it comes to the customization of alert content we have to use popup dialog.

Popup Dialog Component

Popup dialog also provides the following components:

  1. DialogTitle
  2. DialogButton
  3. SlideAnimation
  4. ScaleAnimation
  5. FadeAnimation

This example contains three types of popup dialogs:

  1. Default Animation Dialog
  2. Scale Animation Dialog
  3. Slide Animation Dialog

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use PopupDialog we have to install react-native-popup-dialog package.

To install this open the terminal and jump into your project

Run the following command

This command will copy all the dependencies into your node_module directory, You can find the directory in node_module the directory named react-native-popup-dialog.

–save is optional, it is just to update the react-native-popup-dialog dependancy in your package.json file.

Dependency Version for this Example used is

Now Open App.js in any code editor and replace the code with the following code.

App.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

         

This is how you can make a popup dialog in React Native. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Example of React Native Tab with Latest React Navigation Version 3.+

Here is an Example of React Native Tab for Android and IOS. We will use react-navigation to make a Tab Navigation in this example. Tabs are the most common style of navigation in mobile apps. Tabs can be on the bottom of the screen or on the top (below the header or even instead of a header). We all have seen WhatsApp and it can’t be so easy without Tab. So let’s get started with the example of Tab Navigation.

This example will work for the React Navigation Version 3+. If you are using Version 2+ then please find the comments in the code for the suitable changes or can see our old post React Native Navigation Version 2+ for the help.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To use createMaterialTopTabNavigator you need to install react-navigation and react-native-gesture-handler dependencies.

To install these dependencies open the terminal and jump into your project

Run the following commands

This command will copy all the dependencies into your node_module directory. –save is optional, it is just to update dependencies in your package.json file.

After installing the dependencies you need to link it with your project using

After linking the dependency if you face  Could not Compile Settings File project/android/settings.gradle error while building and installing the project then you can find the solution here.

Project File Structure

To start with this Example you need to create a directory named pages in your project and create two files FirstPge.js and SecondPage.js in it.

Now Open App.js in any code editor and replace the code with the following code.

App.js

Open pages/FirstPage.js in any code editor and the Replace the code with the following code.

FirstPage.js

Open pages/SecondPage.js in any code editor and the Replace the code with the following code.

SecondPage.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Android

    

IOS

That was the React Native Tab. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Bottom Navigation with Navigation Icon from Local Directory

Here is an example of Bottom Navigation with Navigation Icon from Local Directory. This post is an extension of my previous post on How to Make React Native Bottom Navigation with Latest Navigation V. 3+.

In this example, we are making the same layout and flow as the previous one, Bottom Navigation with navigation options like Home and Setting. Each screen has other navigation options too but this time the navigation tab icon will be from the local directory.

This example will work for the React Navigation Version 3+. If you are using Version 2+ then please find the comments in the code for the suitable changes or can see our old post React Native Navigation Version 2+ for the help.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To install dependencies open the terminal and jump into your project

To use createStackNavigator you need to install react-navigation and react-native-gesture-handler dependencies.
Run the following commands

This command will copy all the dependencies into your node_module directory. –save is optional, it is just to update dependencies in your package.json file.

Linking of Dependencies

After installing the dependencies you need to link it with your project using

After linking the dependency if you face  Could not Compile Settings File project/android/settings.gradle error while building and installing the project then you can find the solution here.

Project File Structure

To start with this Example you need to create

  1. A directory named pages in your project and create four files DetailsScreen.js, HomeScreen.js, ProfileScreen.js and SettingScreen.js in it.
  2. A directory named asset which will have our icons stored.

      

Now Open App.js in any code editor and replace the code with the following code.

App.js

Open pages/DetailsScreen.js in any code editor and the Replace the code with the following code.

DetailsScreen.js

Open pages/HomeScreen.js in any code editor and the Replace the code with the following code.

HomeScreen.js

Open pages/ProfileScreen.js in any code editor and the Replace the code with the following code.

ProfileScreen.js

Open pages/SettingsScreen.js in any code editor and the Replace the code with the following code.

SettingsScreen.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

  

      

That was the React Native Bottom Navigation. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

How to Make React Native Bottom Navigation with Latest Navigation V. 3+

Here is an example of React Native Bottom Navigation. Bottom Navigation is very useful when you have 2-3 main navigation options. It provides the user with easy access to regular usable options.

In this example, We will make a Bottom Navigation with navigation options like Home and Setting. Each screen has other navigation options too. So let’s get started.

This example will work for the React Navigation Version 3+. If you are using Version 2+ then please find the comments in the code for the suitable changes or can see our old post React Native Navigation Version 2+ for the help.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To install dependencies open the terminal and jump into your project

1. To use createStackNavigator you need to install react-navigation and react-native-gesture-handler dependencies.
Run the following commands

2. We are going to use Ionicons to show the menu icons from react-native-vector-icons/Ionicons. So to install react-native-vector-icons run the following command

This command will copy all the dependencies into your node_module directory. –save is optional, it is just to update dependencies in your package.json file.

Linking of Dependencies

After installing the dependencies you need to link it with your project using

After linking the dependency if you face  Could not Compile Settings File project/android/settings.gradle error while building and installing the project then you can find the solution here.

Project File Structure

To start with this Example you need to create a directory named pages in your project and create four files DetailsScreen.js, HomeScreen.js, ProfileScreen.js and SettingScreen.js in it.


Now Open App.js in any code editor and replace the code with the following code.

App.js

Open pages/DetailsScreen.js in any code editor and the Replace the code with the following code.

DetailsScreen.js

Open pages/HomeScreen.js in any code editor and the Replace the code with the following code.

HomeScreen.js

Open pages/ProfileScreen.js in any code editor and the Replace the code with the following code.

ProfileScreen.js

Open pages/SettingsScreen.js in any code editor and the Replace the code with the following code.

SettingsScreen.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Android

(Home menu with details screen in it)

    

IOS

(Home menu with details screen in it)

    

Android

(Setting menu with detail and profile screens in it)

           

IOS

(Setting menu with detail and profile screens in it)

          

In this example, we have used Ionicons for the bottom navigation tab icons. If you want to use the icons from the local directory of your project then please visit our next post on Bottom Navigation with Navigation Icon from Local Directory. This will be a similar example but the bottom tab icon will be from the project’s local directory.

That was the React Native Bottom Navigation. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

React Native Finish Current Screen while Navigating to Next Screen

While we navigate to the next screen from any screen the old screen remains in the stack and after coming back we see the same screen. React Native Finish Current Screen while Navigating to Next Screen includes how we navigate to next screen and finish the old one from the stack?

Why do we need to finish the current screen while navigating to next screen?

While developing an App we sometimes need to go to the next screen but never want to come back to the current screen. For an example, You have a Splash screen in your App which you want to show for the first time when the user opens the App and do not want to show it again either by the pressing the back button or by any other way.

To finish the current screen while navigating to next screen you have to define the navigation of the screen by the createSwitchNavigator in place of createStackNavigator. It will not store your previous screen into the stack so the user will not be able to go to the previous screen.

If you have used the createStackNavigator you can find that in the First screen, the app has a Navigation Bar and while going to the other screen it will have a Navigation Bar with the back button to go to the previous screen. It is because it stores the previous screen in the stack to go back. But in this example of createSwitchNavigator, you can find it do not show the Navigation Bar either on First Screen or on Second Screen. So let’s get started with the example.

This example will work for the React Navigation Version 3+. If you are using Version 2+ then please find the comments in the code for the suitable changes or can see our old post React Native Navigation Version 2+ for the help.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To use createStackNavigator you need to install react-navigation and react-native-gesture-handler dependencies.

To install these dependencies open the terminal and jump into your project

Run the following commands

This command will copy all the dependencies into your node_module directory. –save is optional, it is just to update dependencies in your package.json file.

Linking of Dependencies

After installing the dependencies you need to link it with your project using

After linking the dependency if you face  Could not Compile Settings File project/android/settings.gradle error while building and installing the project then you can find the solution here.

Project File Structure

To start with this Example you need to create a directory named pages in your project and create two files FirstPge.js and SecondPage.js in it.


Now open App.js in any code editor and replace the code with the following code.

App.js

Open pages/FirstPage.js in any code editor and the Replace the code with the following code.

FirstPage.js

Open pages/SecondPage.js in any code editor and the Replace the code with the following code.

SecondPage.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Android

    

IOS

      

That was the React Native Switch Navigator. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Example of React Native Navigation Drawer with Latest Navigation V. 3+

This is an Example of React Native Navigation Drawer for Android and IOS. We will use react-navigation to make a navigation drawer in this example. React Native Navigation Drawer is a very popular component in app development. It provides you to manage the number of app options in a very easy manner. A user can navigate from one screen to another screen very easily by just pulling out the drawer. Here is an example of a navigation drawer.

This example will work for the React Navigation Version 3+. If you are using Version 2+ then please find the comments in the code for the suitable changes or can see our old post React Native Navigation Version 2+ for the help.

Make React Native Navigation Drawer Using

In this example, we will make a navigation drawer with Three screens. So let’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To use createDrawerNavigator, createStackNavigator you need to install react-navigation and react-native-gesture-handler dependencies.

To install these dependencies open the terminal and jump into your project

Run the following commands

This command will copy all the dependencies into your node_module directory. –save is optional, it is just to update dependencies in your package.json file.

After installing the dependencies you need to link it with your project using

After linking the dependency if you face  Could not Compile Settings File project/android/settings.gradle error while building and installing the project then you can find the solution here.

Project Structure

To start with this Example you need to create a directory named pages in your project and create three files Screen1.js, Screen2.js, and Screen3.js in it.

To have the drawer Icon in Action Bar you have to make an image directory in your project and have to copy any drawer image with name drawer.png (For this example. You can change if you want but you need to change the file name in code too).

Here is the drawer.png if you want to download.

Now Open App.js in any code editor and replace the code with the following code.

App.js

Open pages/Screen1.js in any code editor and the Replace the code with the following code.

Screen1.js

Open pages/Screen2.js in any code editor and the Replace the code with the following code.

Screen2.js

Open pages/Screen3.js in any code editor and the Replace the code with the following code.

Screen3.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

IOS

               

ANDROID

               

That was the React Native Navigation Drawer. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

React Native Navigation (Router) Version 2+

React Native Navigation is used to switch from one screen to other in the desired manner. This navigation solution is written entirely in JavaScript (so you can read and understand all of the sources), on top of powerful native primitives.

React Navigation Version 3+ has been launched which needs some updates to navigate. This example will work for the React Navigation Version 2+. If you are using Version 3+ then please see our new post on React Native Navigation Version 3+.

Navigate Using

In this example, we will navigate from the first screen to second on a click of a button. So Let’s get Started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use createStackNavigator you need to install react-navigation package. To install this

Open the terminal and jump into the project

Run the following command

This command will copy all the dependencies into your node_module directory, You can find the directory in node_module the directory named react-navigation.

–save is optional, it is just to update the react-navigation dependancy in your package.json file.

Dependency Version for this Example used is

Project File Structure

To start with this Example you need to create a directory named pages in your project and create two files FirstPge.js and SecondPage.js in it.

Now Open App.js in any code editor and replace the code with the following code.

App.js

Open pages/FirstPage.js in any code editor and replace the code with the following code.

FirstPage.js

Open pages/SecondPage.js in any code editor and replace the code with the following code.

SecondPage.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

IOS

     

Android

     


That was the React Native Navigation. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Handling Android Back Button Press in React Native

When the user presses the Android hardware back button in React Native, react-navigation will pop a screen or exit the app if there are no screens to pop. This is the sensible default behavior, but there are situations when you might want to implement custom handling. This is where handling android back button is useful. You can also check react-navigation-backhandler library to handle the back press.

To handle the Android Back Button Press in the react native we have to register the hardwareBackPress event listener with a callback function, which will be called after pressing the Back Button. We should also remove the listener when we are jumping out from the screen (where we have added the listener) because sometime after going to the next screen or any other screen the listener remains active in the background if the last screen is in the stack and not killed by the system.

The event subscriptions are called in reverse order (i.e. last registered subscription first), and if one subscription returns true then subscriptions registered earlier will not be called. 

1. Add the hardwareBackPress using

2. Remove the hardwareBackPress using

3. To handle the event we have used the function handleBackButtonClick.

Returning true from handleBackButtonClick denotes that we have handled the event, and react-navigation’s listener will not get called, thus not popping the screen. Returning false will cause the event to bubble up and react-navigation’s listener will pop the screen.

Why not use component lifecycle methods?

At first, you may be inclined to use componentDidMount to subscribe for the back press event and componentWillUnmount to unsubscribe. The reason, why we do not use them, is that they are not generally called when entering or leaving a screen.

More specifically, consider a StackNavigator with screens A and B. After navigating to A, its componentDidMount is called. When pushing B, its componentDidMount is also called, but A remains mounted and its componentWillUnmount is therefore not called.

Similarly, when going back from B to AcomponentWillUnmount of B is called, but componentDidMount of A is not because A remained mounted the whole time.

So here was the big discussion. Now Let’s start with the code.

In our example, we are going from FirstPage to SecondPage using navigate and after clicking Back Button on SecondPage we will navigate to ThirdPage with an alert. We have also used this.props.navigation.goBack(null); on ThirdPage to popup the default screen. So let’s get started.

This example will work for the React Navigation Version 3+. If you are using Version 2+ then please find the comments in the code for the suitable changes or can see our old post React Native Navigation Version 2+ for the help.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To use createStackNavigator you need to install react-navigation and react-native-gesture-handler dependencies.

To install these dependencies open the terminal and jump into your project

Run the following commands

This command will copy all the dependencies into your node_module directory. –save is optional, it is just to update dependencies in your package.json file.

Linking of Dependencies

After installing the dependencies you need to link it with your project using

After linking the dependency if you face  Could not Compile Settings File project/android/settings.gradle error while building and installing the project then you can find the solution here.

Project File Structure

To start with this Example you need to create a directory named pages in your project and create three files FirstPge.js, SecondPage.js, and ThirdPage.js.

Now Open App.js in any code editor and replace the code with the following code.

App.js

Open pages/FirstPage.js in any code editor and replace the code with the following code.

FirstPage.js

Open pages/SecondPage.js in any code editor and replace the code with the following code.

SecondPage.js

Open pages/ThirdPage.js in any code editor and replace the code with the following code.

ThirdPage.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

That was the way to handle the Android Back Button. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂