React Native stack navigation

You must know about the stack data structure. Stack is one of the important data structures which works in the FILO(first in last out) method.

As you know, mobile apps are generally not made of only one page, and there are transitions between pages as per user activity. Even a very basic app consists of multiple pages, so the transition is the important part where we need to work keenly. Stack navigation provides a way of transition where a new screen is always placed on the top of the stack. Its implementation can be done in javascript. one thing to keep in mind is that we can customize react-native stack navigation as needed. Animations can be added too and can be controlled. You will be learning how screens are stacked on each other.

Let's work with stack navigation.

We need to install some packages before using the stack navigator, so open your terminal and execute the following command -

  • npm install @react-navigation/stack
  • npm install react-native-gesture-handler
  • npm install react-native-screens
  • react-native-safe-area-context

Make one folder named screen and add three more folders home, options and menu.

Home.js code  :

import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';
 
class Home extends Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' ,backgroundColor:'#ffb3b3',justifyContent: "space-around",flexDirection: "row",padding:10}}>
        
        <Button
          title="go to options"
          onPress={() => this.props.navigation.navigate('Blog')}
          
        />
        <Button
          title="Go to menu"
          onPress={() => this.props.navigation.navigate('BlogDetails')}
          
        />
       
    
      </View>
    );
  }
}
 
export default Home;

Menu.js code:

import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';
 
class menu extends Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center',backgroundColor:'#ffcce0' }}>
        <Text>menu screen</Text>
        <Button
          title="Go to Home"
          onPress={() => this.props.navigation.navigate('Home')}
        />
        <Button
          title="Go to options"
          onPress={() => this.props.navigation.navigate('Blog')}
         
        />
      </View>
    );
  }
}
 
export default menu;

Options.js code :

import React, { Component } from 'react';
import { Button, View, Text } from 'react-native';
 
class options extends Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>option Screen</Text>
        <Button
          title="Go to Home"
          onPress={() => this.props.navigation.navigate('Home')}
        />
        <Button
          title="Go to menu screen"
          onPress={() => this.props.navigation.navigate('BlogDetails')}
        />
      </View>
    );
  }
}
 
export default options;

App.js code:

import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
 
import Home from './Home';
import Blog from './Options';
import BlogDetails from './Menu';
 
const Stack = createStackNavigator();
 
function NavStack() {
  return (
     <Stack.Navigator
        initialRouteName="Home"
        screenOptions={{
          headerTitleAlign: 'center',
          headerStyle: {
            backgroundColor: '#ff66a3',
          },
          headerTintColor: '#fff',
          headerTitleStyle :{
            fontWeight: 'bold',
          },
        }}
      >
      <Stack.Screen
        name="Home"
        component={Home}
        options={{ title: 'welcome to Home screen' }}
      />
      <Stack.Screen
        name="Blog"
        component={Blog}
        options={{ title: 'welcome to option screen' }}
      />
      <Stack.Screen
       name="BlogDetails"
       component={BlogDetails}
       options={{ title: 'welcome to menu screen' }}
      />
    </Stack.Navigator>
  );
}
 
export default function App() {
  return (
    <NavigationContainer>
      <NavStack />
    </NavigationContainer>
  );
}
 
console.disableYellowBox = true;

Output :

 React native stack navigation
 React native stack navigation
 React native stack navigation

Explanation :

Home.js, options.js, and menu.js files are identical. If you understand any one of them, then the other two can be understood very clearly.

Lets look at menu.js code class  component-

class menu extends Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center',backgroundColor:'#ffcce0' }}>
        <Text>menu screen</Text>
        <Button
          title="Go to Home"
          onPress={() => this.props.navigation.navigate('Home')}
        />
        <Button
          title="Go to options"
          onPress={() => this.props.navigation.navigate('Blog')}
         
        />
      </View>
    );
  }
}


export default menu;

Here we have defined the class name as the menu, we have given some necessary styling, and then we have defined the text component, then the main factor is the button, and onPress defines what will happen after clicking the on the button.

<Button
          title="Go to Home"
          onPress={() => this.props.navigation.navigate('Home')}
        />

 After clicking on the button, we will be directed to the Home screen.

 />
        <Button
          title="Go to options"
          onPress={() => this.props.navigation.navigate('options')}
         
        />

After clicking on this, we will be directed to options.

The reason for elaborating this is that suppose if you are on the home page, then you navigated to the options page, and then you navigated to the menu page. You can witness a back option there, so what will happen if you press back now? Where will you be navigated?

So the answer is you will be navigated to the last visited page and so on. That's why it is called stack operation.

So hereafter, clicking back, you will be navigated to the options page, and again after clicking it, you will be navigated to home.


Related Topics

React Native Alert

In this part of the tutorial, we will learn about react-native's alert API. It is compatible with both android and IOS. What is API? Answer - API stands for Application programming interface,...

4 minutes read.

Difference Between React.js and React Native

React js vs. React-native React native:  react-native is an open-source javascript framework that can be used to develop native mobile apps for IOS and android. Facebook released it in 2015 and witnessed...

3 minutes read.

How Does React Native Work

Working of React Native React Native is a framework that lets us write android and iOS mobile applications using JavaScript. It is interesting to see how easy it is to develop...

4 minutes read.

React Native Sound

It is a great way to connect to the user. Just a tiny "ting" of any email received, or some sound for deleting a file or sound for sending a...

8 minutes read.

React Native Side Drawer

In this part of the react-native tutorial, we will be learning about the react-native side drawer. We have already studied the status bar and various parts of react-native navigation, including...

5 minutes read.

TouchableOpacity in React Native

Touchable opacity is the most suited example if you want to make views respond to proper touch. If you want to control the opacity then it can be done by...

3 minutes read.

React Native Creating Components

React elements are where all React codes reside. The React elements are very similar to the regular React components, which have a unique style and offer contrast. Working with views When you...

3 minutes read.

React native calender

In this article, we will be learning to add calendar features to react native app. So before creating the calendar, we do need to install a few packages or add some...

3 minutes read.

Basic knowledge of firebase for React Native

Firebase for react-native : Firebase is a mobile and web application development platform which is created in 2014 by google. It is a product of google which is offered to developers...

7 minutes read.

TouchableHighlight in React Native

It is just a wrapper for making touch work properly, and it is the conventional way of handling touch gestures. Today's most future-proof solution is pressable API. pressable manages all...

2 minutes read.

What is a Bridge in React Native?

A React Native is generally composed of two fields or two parts: The first part is the code of JavaScript.The second one is native code. The ability to build a bridge between...

3 minutes read.

Redux in React Native

 It is a really amazing library of javascript, react, and react-native. It is very popular in react native domain. Every react and react native developer must know the redux it...

7 minutes read.

HTTP in React Native

Networking HTTP in React-native: we all are aware that react native is a javascript framework.HTTP is nothing but an API request, and you might think that what is the need for an...

4 minutes read.

React Native Components

React Native components are the same as ordinary react components, but they are different in terms of rendering and styling. All react code lives in react components. They are independent...

16 minutes read.

React Native API

API API stands for an Application programming interface. API allows communication between two software and is also responsible for data sharing. It takes the asked data from the server and delivers...

6 minutes read.

React Native indicator

React native activity indicator : React native activity indicator is one of the react-native components. We all have seen it several times. It's the loader. Look at the picture below - So now...

4 minutes read.

States in React Native

We can control data of components of react-native by state and props. We are familiar with props and used them multiple times previously. Props: This is used to customize components and...

3 minutes read.

React Native Toast

Introduction: It is a message that will be displayed on the screen for a short span of time to interrupt the user. Primarily it is displayed by any user action....

5 minutes read.

React Native stack navigation

You must know about the stack data structure. Stack is one of the important data structures which works in the FILO(first in last out) method. As you know, mobile apps are...

4 minutes read.

React Native Tutorial

Introduction to react native : React-native is an open-source javascript framework that can be used to develop native mobile apps for IOS and android. It is released by Facebook in 2015...

4 minutes read.