Contents
This is an example of Bottom 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 Bottom Tab View inside the Navigation Drawer.
In this example, we have a navigation drawer with 3 screens in the navigation menu and a Bottom Tab on the first screen of the Navigation Drawer. When we open Screen1 the Bottom Tab will be visible and on the other options, this Bottom Tab will be invisible.
<NavigationContainer>
<Drawer.Navigator
drawerContentOptions={{
activeTintColor: '#e91e63',
itemStyle: { marginVertical: 5 },
}}>
<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>
<Tab.Navigator
initialRouteName="HomeScreen"
tabBarOptions={{
activeTintColor: 'tomato',
inactiveTintColor: 'gray',
style: {
backgroundColor: '#e0e0e0',
},
labelStyle: {
textAlign: 'center',
fontSize: 16
},
}}>
<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>
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 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
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-gesture-handler
, react-native-reanimated
, react-native-screens
and react-native-safe-area-context
and @react-native-community/masked-view
npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view --save
3. For the Drawer Navigator install
npm install @react-navigation/drawer --save
4. For the Bottom Tab Navigator install
npm install @react-navigation/bottom-tabs --save
5. These steps are enough for the drawer navigation but in this example, we are also using between screens so we will also need Stack Navigator
npm install @react-navigation/stack --save
Note: When you use a navigator, you’ll need to follow the installation instructions of that navigator for any additional dependencies. If you’re getting an error “Unable to resolve module”, you need to install that module in your project.
You might get warnings related to peer dependencies after installation. They are usually caused by incorrect version ranges specified in some packages. You can safely ignore most warnings as long as your app builds.
CocoaPods Installation
Please use the following command to install CocoaPods
npx pod-install ios
To finalize the installation of 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 files, such as index.js or App.js:
import 'react-native-gesture-handler';
Note: If you skip this step, your app may crash in production even if it works fine in development.
CocoaPods Installation
After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the library but need to install pods. So to install pods use
npx pod-install ios
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
// React Navigate Drawer with Bottom Tab
// https://aboutreact.com/bottom-tab-view-inside-navigation-drawer/
import 'react-native-gesture-handler';
import * as React from 'react';
import {View, TouchableOpacity, Image} from 'react-native';
import {
NavigationContainer,
getFocusedRouteNameFromRoute,
} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import {createDrawerNavigator} from '@react-navigation/drawer';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import HomeScreen from './pages/HomeScreen';
import ExploreScreen from './pages/ExploreScreen';
import SettingScreen from './pages/SettingScreen';
const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
const Tab = createBottomTabNavigator();
const NavigationDrawerStructure = (props) => {
//Structure for the navigatin Drawer
const toggleDrawer = () => {
//Props to open/close the drawer
props.navigationProps.toggleDrawer();
};
return (
<View style={{flexDirection: 'row'}}>
<TouchableOpacity onPress={() => toggleDrawer()}>
{/*Donute Button Image */}
<Image
source={{
uri:
'https://raw.githubusercontent.com/AboutReact/sampleresource/master/drawerWhite.png',
}}
style={{width: 25, height: 25, marginLeft: 5}}
/>
</TouchableOpacity>
</View>
);
};
const getHeaderTitle = (route) => {
const routeName = getFocusedRouteNameFromRoute(route) ?? 'Feed';
switch (routeName) {
case 'HomeScreen':
return 'Home';
case 'ExploreScreen':
return 'Explore';
case 'BottomTabStack':
return 'Home';
}
};
const BottomTabStack = () => {
return (
<Tab.Navigator
initialRouteName="HomeScreen"
tabBarOptions={{
activeTintColor: 'tomato',
inactiveTintColor: 'gray',
style: {
backgroundColor: '#e0e0e0',
},
labelStyle: {
textAlign: 'center',
fontSize: 16,
},
}}>
<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 = ({navigation}) => {
return (
<Stack.Navigator initialRouteName="HomeScreen">
<Stack.Screen
name="BottomTabStack"
component={BottomTabStack}
options={({route}) => ({
headerTitle: getHeaderTitle(route),
headerLeft: () => (
<NavigationDrawerStructure
navigationProps={navigation}
/>
),
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
headerTintColor: '#fff', //Set Header text color
headerTitleStyle: {
fontWeight: 'bold', //Set Header text style
},
})}
/>
</Stack.Navigator>
);
};
const SettingScreenStack = ({navigation}) => {
return (
<Stack.Navigator
initialRouteName="SecondPage"
screenOptions={{
headerLeft: () => (
<NavigationDrawerStructure navigationProps={navigation} />
),
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
headerTintColor: '#fff', //Set Header text color
headerTitleStyle: {
fontWeight: 'bold', //Set Header text style
},
}}>
<Stack.Screen
name="SettingScreen"
component={SettingScreen}
options={{
title: 'Setting', //Set Header Title
}}
/>
</Stack.Navigator>
);
};
const App = () => {
return (
<NavigationContainer>
<Drawer.Navigator
drawerContentOptions={{
activeTintColor: '#e91e63',
itemStyle: {marginVertical: 5},
}}>
<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>
);
};
export default App;
HomeScreen.js
Open pages/HomeScreen.js in any code editor and replace the code with the following code.
// React Navigate Drawer with Bottom Tab
// https://aboutreact.com/bottom-tab-view-inside-navigation-drawer/
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 Bottom 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.
// React Navigate Drawer with Bottom Tab
// https://aboutreact.com/bottom-tab-view-inside-navigation-drawer/
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 Bottom 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.
// React Navigate Drawer with Bottom Tab
// https://aboutreact.com/bottom-tab-view-inside-navigation-drawer/
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 Bottom 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
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 (macOS only)
react-native run-ios
Output Screenshots
This is how you can add Bottom 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. 🙂
Thanks for useful article
But how to change the fontfamily and fontsize of drawer?
How to generate ipa in react native
You can see https://medium.com/better-programming/create-ipa-and-apk-from-react-native-72fe53c6a8db
Thanks bro