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 will have a navigation drawer with two screens in the navigation menu, two buttons on the first screen to open a screen with navigation drawer and other to open a new screen out of the navigation drawer without navigation drawer.
Switching Screen out of the navigation means we will open the Screen independent of the navigation drawer.
To do this we will create a Drawer.Navigator and then will put it into Stack.Navigator as a Stack.Screen with ScreenExternal. So the Drawer will be a group and ScreenExternal will be an independent entity that will be a part of root stack navigator.
Drawer.Navigator
<Drawer.Navigator>
<Drawer.Screen
name="FirstPage"
options={{
drawerLabel: 'First page Option',
activeTintColor: '#e91e63',
}}
component={firstScreenStack}
/>
<Drawer.Screen
name="SecondPage"
options={{
drawerLabel: 'Second page Option',
activeTintColor: '#e91e63',
}}
component={secondScreenStack}
/>
</Drawer.Navigator>
Stack.Navigator
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={Home}
options={{headerShown: false}}
/>
<Stack.Screen
name="ScreenExternal"
component={ScreenExternal}
options={{
title: 'External Screen', //Set Header Title
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
headerTintColor: '#fff', //Set Header text color
headerTitleStyle: {
fontWeight: 'bold', //Set Header text style
},
}}
/>
</Stack.Navigator>
</NavigationContainer>
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 command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
This will make a project structure with an index file named App.js in your project directory.
Installation of Dependencies
For navigation drawer we need to add react-navigation
and other supporting dependencies.
To install the dependencies open the terminal and jump into your project
cd ProjectName
1. Install react-navigation
npm install @react-navigation/native --save
2. Other supporting libraries react-native-screens
and react-native-safe-area-context
npm install react-native-screens react-native-safe-area-context --save
react-native-screens
package requires one additional configuration step to properly work on Android devices. Edit MainActivity.java
file which is located in android/app/src/main/java/<your package name>/MainActivity.java
.
Add the following code to the body of MainActivity
class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
}
and make sure to add the following import statement at the top of this file below your package statement:
import android.os.Bundle;
This change is required to avoid crashes related to View state being not persisted consistently across Activity restarts.
3. For the Drawer Navigator install
npm install @react-navigation/drawer --save
4. Now we need to install and configure install react-native-gesture-handler
and react-native-reanimated
libraries that is required by the drawer navigator:
npm install react-native-gesture-handler react-native-reanimated --save
To configure react-native-reanimated
add Reanimated’s Babel plugin to your babel.config.js
(Reanimated plugin has to be listed last.)
module.exports = {
presets: [
...
],
plugins: [
... ,
'react-native-reanimated/plugin'
],
};
To configure react-native-gesture-handler
, add the following at the top (make sure it’s at the top and there’s nothing else before it) of your entry file, such as index.js
or App.js
import 'react-native-gesture-handler';
Note: If you are building for Android or iOS, do not skip this step, or your app may crash in production even if it works fine in development. This is not applicable to other platforms.
5. These steps are enough for the drawer navigation but in this example, we are also moving between screens so we will also need Stack Navigator
npm install @react-navigation/native-stack --save
CocoaPods Installation
Please use the following command to install CocoaPods
npx pod-install
Project Structure
To start with this example you need to create a directory named pages in your project and create four files FirstPage.js, SecondPage.js, ScreenExternal.js, and ScreenInternal.js in 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
// Switch Screen out of the Navigation Drawer
// https://aboutreact.com/switch-screen-out-of-the-navigation-drawer-in-react-native/
import 'react-native-gesture-handler';
import * as React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import {createDrawerNavigator} from '@react-navigation/drawer';
import FirstPage from './pages/FirstPage';
import SecondPage from './pages/SecondPage';
import ScreenInternal from './pages/ScreenInternal';
import ScreenExternal from './pages/ScreenExternal';
const Stack = createNativeStackNavigator();
const Drawer = createDrawerNavigator();
const FirstScreenStack = () => {
return (
<Stack.Navigator
initialRouteName="FirstPage"
screenOptions={{headerShown: false}}>
<Stack.Screen name="FirstPage" component={FirstPage} />
<Stack.Screen name="ScreenInternal" component={ScreenInternal} />
</Stack.Navigator>
);
};
const SecondScreenStack = () => {
return (
<Stack.Navigator
initialRouteName="SecondPage"
screenOptions={{headerShown: false}}>
<Stack.Screen name="SecondPage" component={SecondPage} />
</Stack.Navigator>
);
};
const Home = () => {
return (
<Drawer.Navigator
screenOptions={{
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
headerTintColor: '#fff', //Set Header text color
}}>
<Drawer.Screen
name="FirstScreenStack"
options={{
drawerLabel: 'First page Option',
title: 'First page',
activeTintColor: '#e91e63',
}}
component={FirstScreenStack}
/>
<Drawer.Screen
name="SecondScreenStack"
options={{
drawerLabel: 'Second page Option',
title: 'Second page',
activeTintColor: '#e91e63',
}}
component={SecondScreenStack}
/>
</Drawer.Navigator>
);
};
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={Home}
options={{headerShown: false}}
/>
<Stack.Screen
name="ScreenExternal"
component={ScreenExternal}
options={{
title: 'External Screen', //Set Header Title
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
headerTintColor: '#fff', //Set Header text color
headerTitleStyle: {
fontWeight: 'bold', //Set Header text style
},
}}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
Open pages/FirstPage.js in any code editor and replace the code with the following code.
FirstPage.js
// Switch Screen out of the Navigation Drawer
// https://aboutreact.com/switch-screen-out-of-the-navigation-drawer-in-react-native/
import * as React from 'react';
import {
SafeAreaView,
StyleSheet,
View,
Text,
Button
} from 'react-native';
const FirstPage = ({navigation}) => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View style={styles.container}>
<Text style={styles.textStyle}>
Switch Screen out of the Navigation Drawer
{'\n'}
This is the First Page
</Text>
<Text
style={{
marginTop: 30,
fontSize: 16,
textAlign: 'center'
}}>
Switch to an external screen without navigation drawer
</Text>
<Button
title="Open Screen Out of Navigation Drawer"
onPress={() => navigation.navigate('ScreenExternal')}
/>
<Text
style={{
marginTop: 30,
fontSize: 16,
textAlign: 'center'
}}>
Switch to an internal screen with navigation drawer
</Text>
<Button
title="Open Screen with Navigation Drawer"
onPress={() => navigation.navigate('ScreenInternal')}
/>
</View>
<Text style={styles.footerHeading}>
React Navigation Drawer with Sectioned Menu
</Text>
<Text style={styles.footerText}>www.aboutreact.com</Text>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
textStyle: {
fontSize: 18,
textAlign: 'center',
},
footerHeading: {
fontSize: 18,
textAlign: 'center',
color: 'grey',
},
footerText: {
fontSize: 16,
textAlign: 'center',
color: 'grey',
},
});
export default FirstPage;
Open pages/SecondPage.js in any code editor and replace the code with the following code.
SecondPage.js
// Switch Screen out of the Navigation Drawer
// https://aboutreact.com/switch-screen-out-of-the-navigation-drawer-in-react-native/
import * as React from 'react';
import {
SafeAreaView,
StyleSheet,
View,
Text
} from 'react-native';
const SecondPage = () => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View style={styles.container}>
<Text style={styles.textStyle}>
Switch Screen out of the Navigation Drawer
{'\n\n'}
This is the Second Page
</Text>
</View>
<Text style={styles.footerHeading}>
React Navigation Drawer with Sectioned Menu
</Text>
<Text style={styles.footerText}>www.aboutreact.com</Text>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
textStyle: {
fontSize: 18,
textAlign: 'center',
},
footerHeading: {
fontSize: 18,
textAlign: 'center',
color: 'grey',
},
footerText: {
fontSize: 16,
textAlign: 'center',
color: 'grey',
},
});
export default SecondPage;
Open pages/ScreenExternal.js in any code editor and replace the code with the following code.
ScreenExternal.js
// Switch Screen out of the Navigation Drawer
// https://aboutreact.com/switch-screen-out-of-the-navigation-drawer-in-react-native/
import * as React from 'react';
import {
SafeAreaView,
StyleSheet,
View,
Text,
Button
} from 'react-native';
const ScreenExternal = ({navigation}) => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View style={styles.container}>
<Text style={styles.textStyle}>
Switch Screen out of the Navigation Drawer
{'\n\n'}
This is External Screen
</Text>
<Button
title="Screen Internal"
onPress={() => navigation.navigate('ScreenInternal')}
/>
</View>
<Text style={styles.footerHeading}>
React Navigation Drawer with Sectioned Menu
</Text>
<Text style={styles.footerText}>www.aboutreact.com</Text>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
textStyle: {
fontSize: 18,
textAlign: 'center',
},
footerHeading: {
fontSize: 18,
textAlign: 'center',
color: 'grey',
},
footerText: {
fontSize: 16,
textAlign: 'center',
color: 'grey',
},
});
export default ScreenExternal;
Open pages/ScreenInternal.js in any code editor and replace the code with the following code.
ScreenInternal.js
// Switch Screen out of the Navigation Drawer
// https://aboutreact.com/switch-screen-out-of-the-navigation-drawer-in-react-native/
import * as React from 'react';
import {
SafeAreaView,
StyleSheet,
View,
Text,
Button
} from 'react-native';
const ScreenInternal = ({navigation}) => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View style={styles.container}>
<Text style={styles.textStyle}>
Switch Screen out of the Navigation Drawer
{'\n\n'}
This is Internal Screen
</Text>
<Button
title="Go Back"
onPress={() => navigation.navigate('FirstPage')}
/>
</View>
<Text style={styles.footerHeading}>
React Navigation Drawer with Sectioned Menu
</Text>
<Text style={styles.footerText}>www.aboutreact.com</Text>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
textStyle: {
fontSize: 18,
textAlign: 'center',
},
footerHeading: {
fontSize: 18,
textAlign: 'center',
color: 'grey',
},
footerText: {
fontSize: 16,
textAlign: 'center',
color: 'grey',
},
});
export default ScreenInternal;
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
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. 🙂
Thanks a lot..great tutorial sir
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.
You can use headerRight instead of headerLeft in the same example and you will get the same what you want.
Great tutorial thanks for your great work