Contents
React Native Tab
Here is an example of React Native Tab for Android and iOS using React Navigation V5. We will use react-navigation to make a Tab Navigation in this example. Tabs are the most common style of navigation in mobile apps. Tabs can be on the bottom of the screen or on the top (below the header or even instead of a header). We all have seen WhatsApp and it can’t be so easy without Tab. So let’s get started with the example of Tab Navigation.
This example is updated for the React Navigation V5. For the React Navigation V4, you can scroll to the bottom.
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 React Native Tab 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 Tab Navigator install
npm install @react-navigation/material-top-tabs react-native-tab-view
4. These steps are enough for the Tab navigation but in this example, we are also using Stack Navigator so for that
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
cd ios && pod install && cd ..
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.
Project File Structure
To start with this Example you need to create a directory named pages in your project and create two files FirstPge.js and SecondPage.js in it.
Code to Create React Native Tab
App.js
Now Open App.js in any code editor and replace the code with the following code.
// React Native Tab
// https://aboutreact.com/react-native-tab/
import 'react-native-gesture-handler';
import * as React from 'react';
import {
NavigationContainer
} from '@react-navigation/native';
import {
createStackNavigator
} from '@react-navigation/stack';
import {
createMaterialTopTabNavigator
} from '@react-navigation/material-top-tabs';
/*import
MaterialCommunityIcons
from 'react-native-vector-icons/MaterialCommunityIcons';*/
import FirstPage from './pages/FirstPage';
import SecondPage from './pages/SecondPage';
const Stack = createStackNavigator();
const Tab = createMaterialTopTabNavigator();
function TabStack() {
return (
<Tab.Navigator
initialRouteName="Feed"
tabBarOptions={{
activeTintColor: '#FFFFFF',
inactiveTintColor: '#F8F8F8',
style: {
backgroundColor: '#633689',
},
labelStyle: {
textAlign: 'center',
},
indicatorStyle: {
borderBottomColor: '#87B56A',
borderBottomWidth: 2,
},
}}>
<Tab.Screen
name="FirstPage"
component={FirstPage}
options={{
tabBarLabel: 'Home',
// tabBarIcon: ({ color, size }) => (
// <MaterialCommunityIcons
// name="home"
// color={color}
// size={size}
// />
// ),
}} />
<Tab.Screen
name="SecondPage"
component={SecondPage}
options={{
tabBarLabel: 'Setting',
// tabBarIcon: ({ color, size }) => (
// <MaterialCommunityIcons
// name="settings"
// color={color}
// size={size}
// />
// ),
}} />
</Tab.Navigator>
);
}
function App() {
return (
<NavigationContainer>
<Stack.Navigator
initialRouteName="Settings"
screenOptions={{
headerStyle: { backgroundColor: '#633689' },
headerTintColor: '#fff',
headerTitleStyle: { fontWeight: 'bold' }
}}>
<Stack.Screen
name="TabStack"
component={TabStack}
options={{ title: 'Tab Stack' }}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
FirstPage.js
Open pages/FirstPage.js in any code editor and place the following code.
// React Native Tab
// https://aboutreact.com/react-native-tab/
import * as React from 'react';
import {
TouchableOpacity,
StyleSheet,
View,
Text,
SafeAreaView
} from 'react-native';
const FirstPage = ({ 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{'\n'}(You are on FirstPage)
</Text>
<TouchableOpacity
style={styles.button}
onPress={
() => navigation.navigate('SecondPage')
}>
<Text>Go to settng Tab</Text>
</TouchableOpacity>
</View>
<Text
style={{
fontSize: 18,
textAlign: 'center',
color: 'grey'
}}>
React Native Tab Navigation
</Text>
<Text
style={{
fontSize: 16,
textAlign: 'center',
color: 'grey'
}}>
www.aboutreact.com
</Text>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
button: {
alignItems: 'center',
backgroundColor: '#DDDDDD',
padding: 10,
width: 300,
marginTop: 16,
},
});
export default FirstPage;
SecondPage.js
Open pages/SecondPage.js in any code editor and place the following code.
// React Native Tab
// https://aboutreact.com/react-native-tab/
import * as React from 'react';
import {
TouchableOpacity,
StyleSheet,
View,
Text,
SafeAreaView
} from 'react-native';
const SecondPage = ({ 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{'\n'}(You are on SecondPage)
</Text>
<TouchableOpacity
style={styles.button}
onPress={
() => navigation.navigate('FirstPage')
}>
<Text>Go to Home Tab</Text>
</TouchableOpacity>
</View>
<Text
style={{
fontSize: 18,
textAlign: 'center',
color: 'grey'
}}>
React Native Tab Navigation
</Text>
<Text
style={{
fontSize: 16,
textAlign: 'center',
color: 'grey'
}}>
www.aboutreact.com
</Text>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
button: {
alignItems: 'center',
backgroundColor: '#DDDDDD',
padding: 10,
width: 300,
marginTop: 16,
},
});
export default SecondPage;
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
Android
IOS
Output in Online Emulator
That was the React Native Tab for Android and iOS using React Navigation V5. 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!
I hope you liked it. 🙂
If you are starting a new app or learning for the first time you should follow V5 steps to create Bottom Navigation but if you are still developing the application using React Navigation V4 then you can follow the below steps.
Installation of Dependencies
Please run following commands in your project directory to install the dependencies
npm install react-navigation --save
npx npm install react-native-gesture-handler react-native-safe-area-context @react-native-community/masked-view react-native-screens react-native-reanimated --save
npm install react-navigation-tabs --save
npm install react-navigation-stack --save
cd ios && pod install && cd ..
Code to Create React Native Tab in V4
App.js
//This is an example of React Native Tab
import React from 'react';
//import react in our code.
//Import React Navigation
import {
createAppContainer
} from 'react-navigation';
import {
createMaterialTopTabNavigator
} from 'react-navigation-tabs';
import {
createStackNavigator
} from 'react-navigation-stack';
//Import External Files
import FirstPage from './pages/FirstPage';
import SecondPage from './pages/SecondPage';
const TabScreen = createMaterialTopTabNavigator(
{
Home: { screen: FirstPage },
Settings: { screen: SecondPage },
},
{
tabBarPosition: 'top',
swipeEnabled: true,
animationEnabled: true,
tabBarOptions: {
activeTintColor: '#FFFFFF',
inactiveTintColor: '#F8F8F8',
style: {
backgroundColor: '#633689',
},
labelStyle: {
textAlign: 'center',
},
indicatorStyle: {
borderBottomColor: '#87B56A',
borderBottomWidth: 2,
},
},
}
);
//making a StackNavigator to export as default
const App = createStackNavigator({
TabScreen: {
screen: TabScreen,
navigationOptions: {
headerStyle: {
backgroundColor: '#633689',
},
headerTintColor: '#FFFFFF',
title: 'TabExample',
},
},
});
export default createAppContainer(App);
pages/FirstPage.js
// Home screen
import React, { Component } from 'react';
//import react in our code.
import { Text, View } from 'react-native';
//import all the components we are going to use.
export default class FirstPage extends React.Component {
render() {
return (
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}>
<Text>Home Screen</Text>
</View>
);
}
}
pages/SecondPage.js
// Setting screen
import React, { Component } from 'react';
//import react in our code.
import { Text, View } from 'react-native';
//import all the components we are going to use.
export default class SecondPage extends React.Component {
render() {
return (
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}>
<Text>Setting Screen</Text>
</View>
);
}
}
React Navigation V5 update brings lots of changes in terms of performance and structure. I will suggest you switch to V5 if you are on V4.
Very Good Article sir!!!
I agree with former poster. Verygood article. Only what was necessary and not a lot of extraextra. Thank you!
how do i remove TabExample Header from above because i am using
bottom navigation and drawer navigation too,
Hello Hardik, You can follow these 3 ways:
https://aboutreact.com/react-native-hide-navigation-bar-and-make-screen-full-screen/
Hello Snehal,
Greetings for the day !
Fist of all thank you for looking in my Query
& Thank you very much for this solution.
Thanks & Regards
how can i put the tab inside my component? because i want the tab to be part of my component not the whole screen.
Can you please look this example https://aboutreact.com/tab-view-inside-navigation-drawer-sidebar-with-react-navigation/
hello. ı want to use tab bar in only one page how can ı do that ? when ı put these codes to app.js ıt is working for all pages.
I am unable to understand what you want. Can you visit Tab View inside Navigation Drawer / Sidebar with React Navigation example? May be it can help you.
Thanks bro.. it works
Not Able to scroll the tabbar