Switch Screen out of the Navigation Drawer with Latest Navigation

Introduction

This is an example to Switch Screen out of the Navigation Drawer in React Native. We will use react-navigation to make a navigation drawer in this example. I hope you have already seen our last post on React Native Navigation Drawer because in this post we are just extending the last post to switch out of the Navigation Drawer. In this example, we have a navigation drawer with 3 screens in the navigation menu, a button on the first screen to open a screen with a navigation drawer and a button on the first screen to open a new screen out of the navigation drawer.

Switching Screen out of the navigation means we are opening the ScreenExternal independent of the Navigation Drawer. To do this we are going to make a DrawerNavigator and then will put this Navigator in parallel with ScreenExternal in StackNavigator. So the Drawer will be a group and ScreenExternal will be an independent entity that will be a part of stackNavigator, not the DrawerNavigator.

Make the DrawerNavigator using

const Drawer = createDrawerNavigator({
  Screen1: {
    screen: FirstActivity_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Screen 1 with Button',
    },
  },

  Screen2: {
    screen: Screen2_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Simple Screen 2',
    },
  },

  Screen3: {
    screen: Screen3_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Simple Screen 3',
    },
  },
});

Make StackNavigator using

const DrawerNavigatorExample = createStackNavigator({
  Drawer: { screen: Drawer, navigationOptions: { header: null } },
  ScreenExternal: {
    screen: ScreenExternal,
    navigationOptions: { title: 'Screen External' },
  },
});

In this example, we will make a navigation drawer with Three screens, a ScreenInternal to open in navigation drawer and a ScreenExternal which will open as an independent screen (out of the navigation drawer). 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

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

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

cd ProjectName

1. Install react-navigation dependency to import createAppContainer

npm install react-navigation --save

2. Other supporting libraries for react-navigation

npm install react-native-gesture-handler react-native-safe-area-context @react-native-community/masked-view react-native-screens react-native-reanimated --save

3. Install react-navigation-drawer to import createDrawerNavigator

npm install react-navigation-drawer --save

These steps are enough for the drawer navigation but in this example, we are also using createStackNavigator to switch screens, so please follow the next step also

4. Install react-navigation-stack to import createStackNavigator

npm install react-navigation-stack --save

These commands will copy all the dependencies into your node_module directory.

CocoaPods Installation

Please use the following command to install CocoaPods

cd ios && pod install && cd ..

Project Structure

To start with this example you need to create a directory named pages in your project and create five files Screen1.js, Screen2.js, Screen3.js, ScreenExternal.js, and ScreenInternal.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 it

Code to open a screen out of the Navigation Drawer

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

App.js

/*Example to open a screen out of the Navigation Drawer*/
import React, { Component } from 'react';
import { View, Image, TouchableOpacity } from 'react-native';

//Import React Navigation
import {createAppContainer} from 'react-navigation';
import {createDrawerNavigator} from 'react-navigation-drawer';
import {createStackNavigator} from 'react-navigation-stack';

import Screen1 from './pages/Screen1';
import Screen2 from './pages/Screen2';
import Screen3 from './pages/Screen3';
import ScreenExternal from './pages/ScreenExternal';
import ScreenInternal from './pages/ScreenInternal';

class NavigationDrawerStructure extends Component {
  toggleDrawer = () => {
    this.props.navigationProps.toggleDrawer();
  };
  render() {
    return (
      <View style={{ flexDirection: 'row' }}>
        <TouchableOpacity onPress={this.toggleDrawer.bind(this)}>
          {/*Donute Button Image */}
          <Image
            source={require('./image/drawer.png')}
            style={{ width: 25, height: 25, marginLeft: 5 }}
          />
        </TouchableOpacity>
      </View>
    );
  }
}
const FirstActivity_StackNavigator = createStackNavigator({
  First: {
    screen: Screen1,
    navigationOptions: ({ navigation }) => ({
      title: 'Screen 1 with Button',
      headerLeft: ()=> <NavigationDrawerStructure navigationProps={navigation} />,

      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
  ScreenInternal: {
    screen: ScreenInternal,
    navigationOptions: ({ navigation }) => ({
      title: 'Screen With Navigation Drawer',
      headerLeft: ()=> <NavigationDrawerStructure navigationProps={navigation} />,

      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

const Screen2_StackNavigator = createStackNavigator({
  Second: {
    screen: Screen2,
    navigationOptions: ({ navigation }) => ({
      title: 'Simple Screen 2',
      headerLeft: ()=> <NavigationDrawerStructure navigationProps={navigation} />,

      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

const Screen3_StackNavigator = createStackNavigator({
  Third: {
    screen: Screen3,
    navigationOptions: ({ navigation }) => ({
      title: 'Simple Screen 3',
      headerLeft: ()=> <NavigationDrawerStructure navigationProps={navigation} />,

      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

const Drawer = createDrawerNavigator({
  Screen1: {
    screen: FirstActivity_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Screen 1 with Button',
    },
  },

  Screen2: {
    screen: Screen2_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Simple Screen 2',
    },
  },

  Screen3: {
    screen: Screen3_StackNavigator,
    navigationOptions: {
      drawerLabel: 'Simple Screen 3',
    },
  },
});

const DrawerNavigatorExample = createStackNavigator({
  Drawer: { screen: Drawer, navigationOptions: { header: null } },
  ScreenExternal: {
    screen: ScreenExternal,
    navigationOptions: { title: 'Screen External' },
  },
});
export default createAppContainer(DrawerNavigatorExample);

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

Screen1.js

/*Example to open a screen out of the Navigation Drawer*/
import React, { Component } from 'react';
import { StyleSheet, View, Text, Button } from 'react-native';

export default class Screen1 extends Component {
  render() {
    return (
      <View style={styles.MainContainer}>
        <Text style={{ fontSize: 23 }}> Screen 1 </Text>
        <Button
          title="Go to Other Screen out of the Navigation Drawer"
          onPress={() => this.props.navigation.navigate('ScreenExternal')}
        />
        <View style={{padding:20}}/>
        <Button
          title="Go to Other Screen with Navigation Drawer"
          onPress={() => this.props.navigation.navigate('ScreenInternal')}
        />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  MainContainer: {
    flex: 1,
    padding: 20,
    alignItems: 'center',
    marginTop: 50,
    justifyContent: 'center',
  },
});

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

Screen2.js

/*Example to open a screen out of the Navigation Drawer*/
import React, { Component } from 'react';
import { StyleSheet, View, Text } from 'react-native';

export default class Screen2 extends Component {
  render() {
    return (
      <View style={styles.MainContainer}>
        <Text style={{ fontSize: 23 }}> Screen 2 </Text>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  MainContainer: {
    flex: 1,
    padding: 20,
    alignItems: 'center',
    marginTop: 50,
    justifyContent: 'center',
  },
});

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

Screen3.js

/*Example to open a screen out of the Navigation Drawer*/
import React, { Component } from 'react';
import { StyleSheet, View, Text } from 'react-native';

export default class Screen3 extends Component {
  render() {
    return (
      <View style={styles.MainContainer}>
        <Text style={{ fontSize: 23 }}> Screen 3 </Text>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  MainContainer: {
    flex: 1,
    padding: 20,
    alignItems: 'center',
    marginTop: 50,
    justifyContent: 'center',
  },
});

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

ScreenExternal.js

/*Example to open a screen out of the Navigation Drawer*/
import React, { Component } from 'react';
import { StyleSheet, View, Text, Button } from 'react-native';

export default class ScreenExternal extends Component {
  render() {
    return (
      <View style={{padding:20}}>
        <Text>Switched screen which is out of the Navigation Drawer</Text>
      </View>
    );
  }
}

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

ScreenInternal.js

/*Example to open a screen out of the Navigation Drawer*/
import React, { Component } from 'react';
import { StyleSheet, View, Text, Button } from 'react-native';

export default class ScreenInternal extends Component {
  render() {
    return (
      <View style={{ padding: 20 }}>
        <Text style={{ padding: '30px' }}>
          Switched Screen Which is in the Navigation Drawer
        </Text>
        <Button
          title="Go to Screen1"
          onPress={() => this.props.navigation.navigate('First')}
        />
      </View>
    );
  }
}

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

Output Screenshots

Android

      

IOS

          

Output in Online Emulator

This is how you can Switch Screen out of the Navigation Drawer in React Native. If you have any doubts 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. 🙂


Related Posts

13 thoughts on “Switch Screen out of the Navigation Drawer with Latest Navigation”

  1. Do you have or can you post an example of a basic navigation header with the drawer on the right?
    By basic I mean like when navigating to ScreenExternal with the back arrow on the left and with the drawer on the right so its always visible.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.