img

A Com­plete Guide to React Native Navigation

18 Nov 2022

When look­ing for native app devel­op­ment ser­vices to build a mobile web app for Android or iOS plat­forms, it is essen­tial to have a vet­er­an mobile app devel­op­ment team that can include high-per­form­ing UI wid­gets that are com­mon­ly used in native appli­ca­tions. Some of the com­po­nents include push­but­tons, list views, and more. 

This is essen­tial as mobile users expect a fast and effi­cient way to nav­i­gate the app. And that’s what a good React Native nav­i­ga­tion lets you do, that is, to ensure your users find a fast and effi­cient way to reach their des­ti­na­tion quickly. 

This blog post will show you how to use com­mon­ly used libraries to set up React Native Navigation. 

What is React Native Navigation?

In React Native, React Nav­i­ga­tion is a stand­alone library that allows you to imple­ment nav­i­ga­tion func­tion­al­i­ty into the application.

React Nav­i­ga­tion is writ­ten entire­ly in JavaScript, so it does not rely on native iOS or Android native nav­i­ga­tion APIs. 

Instead, it cre­ates a sub­set of those APIs for fur­ther code execution. 

As a result,  you can inte­grate third-par­ty JS plu­g­ins, which you can cus­tomize accord­ing­ly and eas­i­ly per­form debug­ging. In addi­tion, you don’t need to learn Objective‑C, Swift, Java, Kotlin, etc to build a react native Android or iOS app.

Read also about 6 Tools for Debug­ging React Native

Nav­i­ga­tion Types In React Navigation

1. Draw­er navigation

Drawer navigation

Twit­ter is one of the most pop­u­lar appli­ca­tions that use draw­er nav­i­ga­tion. It is a nav­i­ga­tion pat­tern where you nav­i­gate between and through the screens using a draw­er from the left (or right). In most cas­es, it con­sists of links that allow users to move between screens, and in web appli­ca­tions, it is sim­i­lar to a sidebar.

2. Tab navigation

Tab navigation

Insta­gram is a pop­u­lar appli­ca­tion that uses tab-based nav­i­ga­tion. With this fea­ture being high­ly pop­u­lar, most of the react native app devel­op­ment ser­vice providers deliv­er mobile appli­ca­tions hav­ing Tab nav­i­ga­tion. In this type of nav­i­ga­tion, there are sev­er­al Tab-based com­po­nents features. 

When look­ing at the user inter­faces, Tab nav­i­ga­tion con­sists of two tabs, one at the bot­tom and one at the top. You can use the home screen for the bot­tom tab nav­i­ga­tion. Sim­i­lar­ly, for the top tab nav­i­ga­tion, you can go to the pro­file screen. 

As a result, it offers a remark­able user expe­ri­ence and the devel­op­ment time and devel­op­ment cost are also less com­pared to oth­er React Native Navigation. 

3. Stack navigation

Stack navigation

In stacks, screens get tran­si­tioned between each oth­er where­as mul­ti­ple screens stack on top of one anoth­er. Mov­ing from one screen to anoth­er involves replac­ing one with anoth­er. The What­sApp chat screen is one appli­ca­tion that uses stack navigation.

A mobile appli­ca­tion typ­i­cal­ly uses a vari­ety of nav­i­ga­tors to build the dif­fer­ent screens and func­tion­al­i­ties. These are called nest­ing nav­i­ga­tors and Twit­ter is one of the most pop­u­lar appli­ca­tions which is cur­rent­ly using this nav­i­ga­tion con­cept. Fol­low­ing the trend, sev­er­al native app devel­op­ment com­pa­nies have now incor­po­rat­ed this nav­i­ga­tion trend. 

You too can ask your react native devel­op­ment ser­vices provider to offer a fea­ture where­in users can nav­i­gate between screens with the draw­er nav­i­ga­tion type on the home screen. With the search tab, you can nav­i­gate from the Direct Mes­sages (DMs) screen to a con­ver­sa­tion based on a select­ed Tweet. 

What Are The Pre­req­ui­sites to Get Start­ed with React Navigation?

Ensure that Node.js and NPM are avail­able on your sys­tem before you begin. Using the fol­low­ing com­mand, you can check whether they are installed:

        node ‑v
        npm ‑v

Alter­na­tive­ly, you can just install open source Node.js, and NPM will get installed automatically.

Min­i­mum requirements

  • react-native >= 0.63.0
  • expo >= 41 (if you use Expo)
  • type­script >= 4.1.0 (if you use TypeScript)

Installation​

For React Native projects, you need to install the fol­low­ing packages:

      npm install @react-navigation/native

Installing depen­den­cies

       npm install react-native-screens react-native-safe-area-context

After suc­cess­ful instal­la­tion of all above libraries, get to the side of the project fold­er and start coding.

Code:

First, let’s cre­ate a /components fold­er in our pro­jec­t’s root fold­er. Next, we cre­ate a file called HomeScreen.js. Inside this HomeScreen.js file, enter the fol­low­ing code.

      import * as React from react’;
      import { But­ton, View, Text } from react-native’;
      import { Nav­i­ga­tion­Con­tain­er } from ‘@react-navigation/native’;
      import { cre­at­e­Na­tiveS­tack­Nav­i­ga­tor } from ‘@react-navigation/native-stack’;

      func­tion Home­Screen({ nav­i­ga­tion }) {
        return (
          <View style={{ flex: 1, alig­nItems: cen­ter’, jus­ti­fy­Con­tent: cen­ter’ }}>
          <Text>Home Screen</Text>
            <But­ton
              title=“Go to Detail Page”
              onPress={() => navigation.navigate(‘Details’)}
            />
          </View>
      );
      }

      func­tion DetailsScreen() {
        return (
          <View style={{ flex: 1, alig­nItems: cen­ter’, jus­ti­fy­Con­tent: cen­ter’ }}>
            <Text>Details Screen</Text>
        </View>
        );
      }

      con­st Stack = createNativeStackNavigator();

      func­tion App() {
        return (
        <Nav­i­ga­tion­Con­tain­er>
            <Stack.Navigator initialRouteName=“Home”>
              <Stack.Screen name=“Home” component={HomeScreen} />
              <Stack.Screen name=“Details” component={DetailsScreen} />
          </Stack.Navigator>
        </NavigationContainer>
        );
      }

      export default App;

After run­ning the above sin­gle code­base on the device, the out­put looks like this.

Inside-img4

Code Summary​

  • Nav­i­ga­tion. navigate(‘RouteName’) adds the route to the native stack nav­i­ga­tor if it does not already exist, oth­er­wise, it jumps to the route in the native stack navigator.
  • We can call navigation.push(‘RouteName’) repeat­ed­ly, and it will keep push­ing routes.
  • A back but­ton will auto­mat­i­cal­ly appear in the head­er bar, but you can go back pro­gram­mat­i­cal­ly by call­ing navigation.goBack(). Hard­ware back but­tons work as expect­ed on Android.
  • Stacks can get nav­i­gat­ed with navigation.navigate(‘RouteName’).

 Conclusion

This guide is intend­ed to help you under­stand the React Native Nav­i­ga­tion library and use it more effec­tive­ly. Also, this guide cov­ers the three basic com­po­nents that make up the React Native Nav­i­ga­tion library: 

  1. Stack­Nav­i­ga­tor
  2. Tab­Nav­i­ga­tor
  3. Draw­er­Nav­i­ga­tor. 

We looked at how to ini­tial­ize these com­po­nents, change routes, han­dle screens, and pass data between screens. Fur­ther, we also focused on some of the tools pro­vid­ed by the library for con­ve­nient nav­i­ga­tion with­in your apps, such as screen nav­i­ga­tors, screen con­fir­ma­tions, and screen guards.

The best thing about the React Native Nav­i­ga­tion library is that it is com­pat­i­ble with both the iOS and Android plat­forms. If you are not sure which react native nav­i­ga­tion fea­ture will suit best for your app, you can reach out to us, we are rec­og­nized as the top react native app devel­op­ment com­pa­ny in the USA

Read also: What is React Navigator?

FAQs

1. Which nav­i­ga­tion is best for React Native?

There are three great libraries for React Native mobile apps regard­ing nav­i­ga­tion and routing.

  • React Nav­i­ga­tion
  • React Native Nav­i­ga­tion by Wix
  • React Router Native

2. How does React Native han­dle navigation?

React Native Nav­i­ga­tion is one of the most pop­u­lar alter­na­tives to React Nav­i­ga­tion library. It pro­vides spe­cif­ic fea­tures to work with nav­i­ga­tion in React Native app devel­op­ment. Unlike oth­er React Nav­i­ga­tion libraries, React Native Nav­i­ga­tion direct­ly uses native nav­i­ga­tion APIs on iOS and Android, allow­ing for a more native nav­i­ga­tion experience.

3. How does nav­i­ga­tion work in React Native?

Using JavaScript, React Nav­i­ga­tion allows you to cre­ate com­po­nents and nav­i­ga­tion pat­terns that appear and behave as if they were native. 

React Nav­i­ga­tion uses stack nav­i­ga­tors to man­age the nav­i­ga­tion his­to­ry and dis­play the appro­pri­ate screen based on the route tak­en by the user inside the app.

4. How does React nav­i­ga­tion dif­fer from React Native navigation?

React Nav­i­ga­tion pro­vides a near-native expe­ri­ence using a com­bi­na­tion of libraries, while React Nav­i­ga­tion Native uses native frag­ments for every screen. When select­ing a nav­i­ga­tion library for your application.

5. How do you imple­ment nav­i­ga­tion in React Native?

The fol­low­ing steps will give you  ideas about imple­ment­ing nav­i­ga­tion in React Native.

  • Step 1: Install React Native
  • Step 2: Cre­ate two dif­fer­ent screens for the project.
  • Step 3: Install the React Nav­i­ga­tion package.
  • Step 4: Insert a Nav­i­ga­tion But­ton in Settings.js.
  • Step 5: Reload the application.

6. How do I nav­i­gate to the pre­vi­ous screen in React Native?

In React Native, you can go back to one screen using the goB­ack() method. 

The react-nav­i­ga­tion library’s goB­ack() method is one of its most impor­tant. Using it, you can return to a pre­vi­ous screen in your nav­i­ga­tion stack.

Frequently Asked Questions

Share this post :