How to Hide Navigation Option from Navigation Drawer / Sidebar

Introduction

This post is about How to Hide Navigation Option from Navigation Drawer / Sidebar 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 hide the navigation option from the Navigation Drawer.

Hide Navigation Drawer option means while creating navigation drawer we have a screen in our navigation drawer which should be visible in the navigation option in the left menu but with some changes, we can also control the visibility of the option.

This option is very useful when you have to manage the visibility of the options while the user is logged in or logged out.

In this example, we have a navigation drawer with 4 screens but only 3 navigation options available in the Drawer Menu. On the first screen, we will have a button to open a screen which is a part of the navigation drawer but it is not visible in the Drawer Menu options.

To Hide the Navigation Option from the Navigation Drawer

To hide the navigation option from the Navigation Drawer we have to return a custom drawer label. Have a look in the code snippet below.

//Custom Label Component for the hidden screen
//We will return null for the label thus we will hide the screen to be indexed in sidebar/Drawer
class Hidden extends React.Component {
  render() {
    return null;
  }
}
//Drawer Navigator for the Navigation Drawer / Sidebar
const DrawerNavigatorExample = 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',
    },
  },
  HiddenScreen: {
    screen: HiddenScreen_StackNavigator,
    navigationOptions: {
      //Return Custom label with null in return.
      //It will hide the label from Navigation Drawer
      drawerLabel: <Hidden />,
    },
  },
});

Let’s get started with the example.

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 four files Screen1.js, Screen2.js, Screen3.js, HiddenScreen.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 (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

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

App.js

/*Example to keep screen in navigation drawer but hide its name from the
navigation drawer/ sidebar options*/
import React, { Component } from 'react';
import {
  StyleSheet,
  View,
  Text,
  Image,
  TouchableOpacity,
  Button,
} from 'react-native';

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

//Import all the screens
import Screen1 from './pages/Screen1';
import Screen2 from './pages/Screen2';
import Screen3 from './pages/Screen3';
import HiddenScreen from './pages/HiddenScreen';

//Navigation Drawer Structure for all screen
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>
    );
  }
}

//Stack Navigator for First Option of Navigation Drawer
const FirstActivity_StackNavigator = createStackNavigator({
  First: {
    screen: Screen1,
    navigationOptions: ({ navigation }) => ({
      title: 'Screen 1 with Button',
      headerLeft: ()=> <NavigationDrawerStructure navigationProps={navigation} />,
      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

//Stack Navigator for Hidden Option of Navigation Drawer
const HiddenScreen_StackNavigator = createStackNavigator({
  HiddenScreen: {
    screen: HiddenScreen,
    navigationOptions: ({ navigation }) => ({
      title: 'HiddenScreen',
      headerLeft: ()=> <NavigationDrawerStructure navigationProps={navigation} />,
      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

//Stack Navigator for Second Option of Navigation Drawer
const Screen2_StackNavigator = createStackNavigator({
  Second: {
    screen: Screen2,
    navigationOptions: ({ navigation }) => ({
      title: 'Simple Screen 2',
      headerLeft: ()=> <NavigationDrawerStructure navigationProps={navigation} />,
      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

//Stack Navigator for Third Option of Navigation Drawer
const Screen3_StackNavigator = createStackNavigator({
  Third: {
    screen: Screen3,
    navigationOptions: ({ navigation }) => ({
      title: 'Simple Screen 3',
      headerLeft: ()=> <NavigationDrawerStructure navigationProps={navigation} />,
      headerStyle: {
        backgroundColor: '#FF9800',
      },
      headerTintColor: '#fff',
    }),
  },
});

//Custom Label Component for the hidden screen
//We will return null for the label thus we will hide the screen to be indexed in sidebar/Drawer
class Hidden extends React.Component {
  render() {
    return null;
  }
}
//Drawer Navigator for the Navigation Drawer / Sidebar
const DrawerNavigatorExample = 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',
    },
  },
  HiddenScreen: {
    screen: HiddenScreen_StackNavigator,
    navigationOptions: {
      //Return Custom label with null in return.
      //It will hide the label from Navigation Drawer
      drawerLabel: <Hidden />,
    },
  },
});
export default createAppContainer(DrawerNavigatorExample);

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

Screen1.js

/*Example to keep screen in navigation drawer but hide its name from the
navigation drawer/ sidebar options*/
import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, Text,Button } from 'react-native';
// import all basic components

export default class Screen1 extends Component {
  render() {
    return (
      <View style={styles.MainContainer}>
        <Text style={{ fontSize: 23, marginBottom: 15 }}> Screen 1 </Text>
        <Text style={{ textAlign: 'center', marginBottom: 15 }}>
          Clicking on the button will open the screen which is in the
          Drawernavigator but the name of the screen is not visible in the
          navigation option as we have hidden it
        </Text>
        <Button
          title="Open Hidden Screen in drawer"
          onPress={() => this.props.navigation.navigate('HiddenScreen')}
        />
      </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 replace the code with the following code.

Screen2.js

/*Example to keep screen in navigation drawer but hide its name from the
navigation drawer/ sidebar options*/
import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, Text } from 'react-native';
// import all basic components

export default class Screen2 extends Component {
  //Screen2 Component
  render() {
    return (
      <View style={styles.MainContainer}>
        <Text style={{ fontSize: 23 }}> Screen 2 </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  MainContainer: {
    flex: 1,
    paddingTop: 20,
    alignItems: 'center',
    marginTop: 50,
    justifyContent: 'center',
  },
});

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

Screen3.js

/*Example to keep screen in navigation drawer but hide its name from the
navigation drawer/ sidebar options*/
import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, Text } from 'react-native';
// import all basic components

export default class Screen3 extends Component {
  //Screen3 Component
  render() {
    return (
      <View style={styles.MainContainer}>
        <Text style={{ fontSize: 23 }}> Screen 3 </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  MainContainer: {
    flex: 1,
    paddingTop: 20,
    alignItems: 'center',
    marginTop: 50,
    justifyContent: 'center',
  },
});

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

HiddenScreen.js

/*Example to keep screen in navigation drawer but hide its name from the
navigation drawer/ sidebar options*/
import React, { Component } from 'react';
//import react in our code.
import { StyleSheet, View, Text, Button } from 'react-native';
// import all basic components

export default class HiddenScreen extends React.Component {
  render() {
    return (
      <View style={{ padding: 20 }}>
        <Text style={{ fontSize: 18, textAlign: 'center', marginBottom: 15 }}>
          This is the hidden screen which is in the Drawernavigator but the name
          of the screen is not visible in the navigation option as we have
          hidden it
        </Text>
        <Button
          title="Back to screen1"
          onPress={() => this.props.navigation.navigate('Screen1')}
        />
      </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 to Hide Navigation Option from Navigation Drawer / Sidebar 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. 🙂


Related Posts

1 thought on “How to Hide Navigation Option from Navigation Drawer / Sidebar”

  1. Hi ,why styling is not applying when i used the exact code..navigator image is not displaying and toggling is not working and header with orange color not showing and the navbar header still showing…Kindly please help me out

    Reply

Leave a Comment

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