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 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.

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 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.

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.

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.

Lifecycle of React Native Components

We are familiar with the word “lifecycle”. In human life, the lifecycle has three phases - birthlifedeath In react native its component lifecycle is almost the same as the human lifecycle. The...

6 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.

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 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.

React Native Animations

Animation: animation is a method in which puppets, figures, drawings, etc., appear as moving creatures, making an illusion that things are moving. It is a unique way of expressing something...

6 minutes read.

Advantages and Disadvantages of React Native

Advantages of using React Native It is observed that React Native stands out from most of the existing methods of cross-platform application development, like Ionic or Cordova, because React Native renders...

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.

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.

Why to Learn React Native

The demand for smartphone applications is ever-changing. The elder ones disappear from our remembrances, and smartphones and new applications come each day. In this turbulent ecosystem, it is struggling for...

4 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.

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.

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 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 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 Status Bar

What is the status bar? The simplest and easiest answer to this question can be answered by looking at your mobile phone's top section. What can you see? Most probably, it's...

4 minutes read.