Tab View + Navigation Drawer
This is an example of Tab View inside Navigation Drawer / Sidebar with React Navigation in React Native. We will use react-navigation to make a navigation drawer and Tab in this example. I hope you have already seen our post on React Native Navigation Drawer because in this post we are just extending the last post to show the Tab View inside the Navigation Drawer.
In this example, we have a navigation drawer with 3 screens in the navigation menu and a Tab View on the first screen of the Navigation Drawer. When we open Screen1 the Tab will be visible and on the other options, this Tab will be invisible.
To Create a Drawer Navigator
<NavigationContainer>
<Drawer.Navigator>
<Drawer.Screen
name="HomeScreenStack"
options={{ drawerLabel: 'Home Screen Option' }}
component={HomeScreenStack} />
<Drawer.Screen
name="SettingScreenStack"
options={{ drawerLabel: 'Setting Screen Option' }}
component={SettingScreenStack} />
</Drawer.Navigator>
</NavigationContainer>
To Create Material Top Tab Navigator
<Tab.Navigator initialRouteName="HomeScreen">
<Tab.Screen
name="HomeScreen"
component={HomeScreen}
options={{tabBarLabel: 'Home Screen'}} />
<Tab.Screen
name="ExploreScreen"
component={ExploreScreen}
options={{tabBarLabel: 'Explore Screen'}} />
</Tab.Navigator>
In this example, we will make a Tab Navigator inside a Drawer Navigator 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 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. For the Tab Navigator install
npm install @react-navigation/material-top-tabs react-native-tab-view react-native-pager-view --save
6. These steps are enough for the drawer navigation and Tab 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 three files ExploreScreen.js, HomeScreen.js, and SettingScreen.js.
Code
Now Open App.js in any code editor and replace the code with the following code
App.js
// Tab View inside Navigation Drawer
// https://aboutreact.com/tab-view-inside-navigation-drawer-sidebar-with-react-navigation/
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 {createMaterialTopTabNavigator} from '@react-navigation/material-top-tabs';
import HomeScreen from './pages/HomeScreen';
import ExploreScreen from './pages/ExploreScreen';
import SettingScreen from './pages/SettingScreen';
const Stack = createNativeStackNavigator();
const Drawer = createDrawerNavigator();
const Tab = createMaterialTopTabNavigator();
const TabStack = () => {
return (
<Tab.Navigator
initialRouteName="HomeScreen"
screenOptions={{
tabBarActiveTintColor: '#FFFFFF',
tabBarInactiveTintColor: '#F8F8F8',
tabBarStyle: {
backgroundColor: '#f4511e',
},
tabBarIndicatorStyle: {
borderBottomColor: '#87B56A',
borderBottomWidth: 2,
},
}}>
<Tab.Screen
name="HomeScreen"
component={HomeScreen}
options={{
tabBarLabel: 'Home Screen',
/*tabBarIcon: ({color, size}) => (
<MaterialCommunityIcons
name="home"
color={color}
size={size}
/>
),*/
}}
/>
<Tab.Screen
name="ExploreScreen"
component={ExploreScreen}
options={{
tabBarLabel: 'Explore Screen',
/*tabBarIcon: ({color, size}) => (
<MaterialCommunityIcons
name="settings"
color={color}
size={size}
/>
),*/
}}
/>
</Tab.Navigator>
);
};
const HomeScreenStack = () => {
return (
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name="TabStack" component={TabStack} />
</Stack.Navigator>
);
};
const SettingScreenStack = () => {
return (
<Stack.Navigator screenOptions={{headerShown: false}}>
<Stack.Screen name="SettingScreen" component={SettingScreen} />
</Stack.Navigator>
);
};
const App = () => {
return (
<NavigationContainer>
<Drawer.Navigator
screenOptions={{
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
headerTintColor: '#fff', //Set Header text color
}}>
<Drawer.Screen
name="HomeScreenStack"
options={{
drawerLabel: 'Home Screen Option',
title: 'Home Screen',
}}
component={HomeScreenStack}
/>
<Drawer.Screen
name="SettingScreenStack"
options={{
drawerLabel: 'Setting Screen Option',
title: 'Setting Screen',
}}
component={SettingScreenStack}
/>
</Drawer.Navigator>
</NavigationContainer>
);
};
export default App;
HomeScreen.js
Open pages/HomeScreen.js in any code editor and replace the code with the following code.
// Tab View inside Navigation Drawer
// https://aboutreact.com/tab-view-inside-navigation-drawer-sidebar-with-react-navigation/
import * as React from 'react';
import {Button, View, Text, SafeAreaView} from 'react-native';
const HomeScreen = ({navigation}) => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View
style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
}}>
<Text
style={{
fontSize: 25,
textAlign: 'center',
marginBottom: 16,
}}>
Home Screen
</Text>
<Button
onPress={() => navigation.navigate('SettingScreenStack')}
title="Go to Setting Screen"
/>
<Button
onPress={() => navigation.navigate('ExploreScreen')}
title="Go to Explore Screen"
/>
</View>
<Text
style={{
fontSize: 18,
textAlign: 'center',
color: 'grey'
}}>
React Navigate Drawer with Top Tab
</Text>
<Text
style={{
fontSize: 16,
textAlign: 'center',
color: 'grey'
}}>
www.aboutreact.com
</Text>
</View>
</SafeAreaView>
);
};
export default HomeScreen;
ExploreScreen.js
Open pages/ExploreScreen.js in any code editor and replace the code with the following code.
// Tab View inside Navigation Drawer
// https://aboutreact.com/tab-view-inside-navigation-drawer-sidebar-with-react-navigation/
import * as React from 'react';
import {Button, View, Text, SafeAreaView} from 'react-native';
const ExploreScreen = ({navigation}) => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View
style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
}}>
<Text
style={{
fontSize: 25,
textAlign: 'center',
marginBottom: 16,
}}>
Explore Screen
</Text>
<Button
onPress={() => navigation.navigate('SettingScreen')}
title="Go to Setting Screen"
/>
<Button
onPress={() => navigation.navigate('HomeScreen')}
title="Go to Home Screen"
/>
</View>
<Text
style={{
fontSize: 18,
textAlign: 'center',
color: 'grey'
}}>
React Navigate Drawer with Top Tab
</Text>
<Text
style={{
fontSize: 16,
textAlign: 'center',
color: 'grey'
}}>
www.aboutreact.com
</Text>
</View>
</SafeAreaView>
);
};
export default ExploreScreen;
SettingScreen.js
Open pages/SettingScreen.js in any code editor and replace the code with the following code.
// Tab View inside Navigation Drawer
// https://aboutreact.com/tab-view-inside-navigation-drawer-sidebar-with-react-navigation/
import * as React from 'react';
import {Button, View, Text, SafeAreaView} from 'react-native';
const SettingScreen = ({navigation}) => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View
style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
}}>
<Text
style={{
fontSize: 25,
textAlign: 'center',
marginBottom: 16,
}}>
Setting Screen
</Text>
<Button
onPress={() => navigation.navigate('HomeScreenStack')}
title="Go to Home Stack"
/>
<Button
onPress={() => navigation.navigate('HomeScreen')}
title="Go to Home Screen"
/>
<Button
onPress={() => navigation.navigate('ExploreScreen')}
title="Go to Explore Screen"
/>
</View>
<Text
style={{
fontSize: 18,
textAlign: 'center',
color: 'grey'
}}>
React Navigate Drawer with Top Tab
</Text>
<Text
style={{
fontSize: 16,
textAlign: 'center',
color: 'grey'
}}>
www.aboutreact.com
</Text>
</View>
</SafeAreaView>
);
};
export default SettingScreen;
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
This is how you can add Tab View inside Navigation Drawer / Sidebar with React Navigation 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. 🙂
Hi ,it is working fine.But how can I navigate to normal screen which is not under the tabs or drawer? Kindly help me.
I have 2 screens(screen1,screen2) under tab.And tab is under my sidemenu screen.
Finally I created a new independent screen3.
Now I want to navigate from screen1 to screen3.I tried with this.property.navigation.navigate(‘screen3’).But not working.
Please help me..
Put the screen 3 in createStackNavigator parallel with createDrawerNavigator. Please have a look at Switch Screen out of the Navigation Drawer with Latest Navigation
Hi,
Is any way to sync with drawer and tab navigation? I have three items, A/B/C, and I would like to have these three items shown in tab and drawer navigation. When I click the item B in drawer, the tab B content is show. When i move to tab item C, the drawer is indexed to item C. Is it possible to do this? Any hint or recommendation is appreciated. Thanks.
I haven’t tried this kind of thing, Let me try it once.