React Native Bottom Navigation
Here is an example of Bottom Navigation with Navigation Icon from Local Directory. This post is an extension of my previous post on How to Make React Native Bottom Navigation with Latest Navigation.
In this example, we are making the same layout and flow as the previous one, Bottom Navigation with navigation options like Home and Setting. Each screen has other navigation options too but this time the navigation tab icon will be from the local directory.
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 React Native Bottom 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 Bottom Navigator install
npm install @react-navigation/bottom-tabs --save
4. These steps are enough for the bottom navigation but in this example, we are also moving/switching 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 File Structure
To start with this Example you need to create
- A directory named pages in your project and create four files DetailsScreen.js, HomeScreen.js, ProfileScreen.js and SettingScreen.js in it
- A directory named asset which will have our icons stored
Code
Now Open App.js in any code editor and replace the code with the following code
App.js
// Bottom Navigation with Navigation Icon from Local Directory
// https://aboutreact.com/react-native-bottom-navigation-icon-from-local/
import React from 'react';
import {Image} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import HomeScreen from './pages/HomeScreen';
import DetailsScreen from './pages/DetailsScreen';
import ProfileScreen from './pages/ProfileScreen';
import SettingsScreen from './pages/SettingsScreen';
const Stack = createNativeStackNavigator();
const Tab = createBottomTabNavigator();
function HomeStack() {
return (
<Stack.Navigator
initialRouteName="Home"
screenOptions={{headerShown: false}}>
<Stack.Screen
name="Home"
component={HomeScreen}
options={{title: 'Home Page'}}
/>
<Stack.Screen
name="Details"
component={DetailsScreen}
options={{title: 'Details Page'}}
/>
</Stack.Navigator>
);
}
function SettingsStack() {
return (
<Stack.Navigator
initialRouteName="Settings"
screenOptions={{headerShown: false}}>
<Stack.Screen
name="Settings"
component={SettingsScreen}
options={{title: 'Setting Page'}}
/>
<Stack.Screen
name="Details"
component={DetailsScreen}
options={{title: 'Details Page'}}
/>
<Stack.Screen
name="Profile"
component={ProfileScreen}
options={{title: 'Profile Page'}}
/>
</Stack.Navigator>
);
}
function App() {
return (
<NavigationContainer>
<Tab.Navigator
initialRouteName="Feed"
screenOptions={({route}) => ({
headerStyle: {backgroundColor: '#42f44b'},
headerTintColor: '#fff',
headerTitleStyle: {fontWeight: 'bold'},
tabBarActiveTintColor: 'tomato',
tabBarInactiveTintColor: 'gray',
tabBarIcon: ({focused, color, size}) => {
return (
<Image
source={
focused
? require('./asset/logo2.png')
: require('./asset/logo1.png')
}
style={{
width: size,
height: size,
borderRadius: size,
}}
/>
);
},
})}>
<Tab.Screen
name="HomeStack"
component={HomeStack}
options={{
tabBarLabel: 'Home',
title: 'Home',
}}
/>
<Tab.Screen
name="SettingsStack"
component={SettingsStack}
options={{
tabBarLabel: 'Settings',
title: 'Setting',
}}
/>
</Tab.Navigator>
</NavigationContainer>
);
}
export default App;
Open pages/DetailsScreen.js in any code editor and replace the code with the following code
DetailsScreen.js
// Bottom Navigation with Navigation Icon from Local Directory
// https://aboutreact.com/react-native-bottom-navigation-icon-from-local/
import * as React from 'react';
import {View, Text, SafeAreaView, StyleSheet} from 'react-native';
const DetailsScreen = () => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View style={styles.container}>
<Text style={styles.header}>
You are on Details Screen
</Text>
</View>
<Text style={styles.footerHeading}>
Bottom Navigation with Navigation Icon from Local Directory
</Text>
<Text style={styles.footerText}>www.aboutreact.com</Text>
</View>
</SafeAreaView>
);
};
export default DetailsScreen;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
header: {
fontSize: 25,
textAlign: 'center',
marginBottom: 16,
},
footerHeading: {
fontSize: 18,
textAlign: 'center',
color: 'grey',
},
footerText: {
fontSize: 16,
textAlign: 'center',
color: 'grey',
},
});
Open pages/HomeScreen.js in any code editor and replace the code with the following code
HomeScreen.js
// Bottom Navigation with Navigation Icon from Local Directory
// https://aboutreact.com/react-native-bottom-navigation-icon-from-local/
import * as React from 'react';
import {
TouchableOpacity,
StyleSheet,
View,
Text,
SafeAreaView,
} from 'react-native';
const HomeScreen = ({navigation}) => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View style={styles.container}>
<Text style={styles.header}>You are on Home Screen</Text>
<TouchableOpacity
style={styles.buttonStyle}
onPress={() =>
navigation.navigate(
'SettingsStack',
{screen: 'Settings'}
)
}>
<Text>Go to settng Tab</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.buttonStyle}
onPress={() => navigation.navigate('Details')}>
<Text>Open Details Screen</Text>
</TouchableOpacity>
</View>
<Text style={styles.footerHeading}>
Bottom Navigation with Navigation Icon from Local Directory
</Text>
<Text style={styles.footerText}>www.aboutreact.com</Text>
</View>
</SafeAreaView>
);
};
export default HomeScreen;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
header: {
fontSize: 25,
textAlign: 'center',
marginBottom: 16,
},
footerHeading: {
fontSize: 18,
textAlign: 'center',
color: 'grey',
},
footerText: {
fontSize: 16,
textAlign: 'center',
color: 'grey',
},
buttonStyle: {
alignItems: 'center',
backgroundColor: '#DDDDDD',
padding: 10,
width: 300,
marginTop: 16,
},
});
Open pages/ProfileScreen.js in any code editor and replace the code with the following code
ProfileScreen.js
// Bottom Navigation with Navigation Icon from Local Directory
// https://aboutreact.com/react-native-bottom-navigation-icon-from-local/
import * as React from 'react';
import {View, Text, SafeAreaView, StyleSheet} from 'react-native';
const ProfileScreen = () => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View style={styles.container}>
<Text style={styles.header}>
You are on Profile Screen
</Text>
</View>
<Text style={styles.footerHeading}>
Bottom Navigation with Navigation Icon from Local Directory
</Text>
<Text style={styles.footerText}>www.aboutreact.com</Text>
</View>
</SafeAreaView>
);
};
export default ProfileScreen;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
header: {
fontSize: 25,
textAlign: 'center',
marginBottom: 16,
},
footerHeading: {
fontSize: 18,
textAlign: 'center',
color: 'grey',
},
footerText: {
fontSize: 16,
textAlign: 'center',
color: 'grey',
},
});
Open pages/SettingsScreen.js in any code editor and replace the code with the following code
SettingsScreen.js
// Bottom Navigation with Navigation Icon from Local Directory
// https://aboutreact.com/react-native-bottom-navigation-icon-from-local/
import * as React from 'react';
import {
TouchableOpacity,
StyleSheet,
View,
Text,
SafeAreaView,
} from 'react-native';
const SettingsScreen = ({route, navigation}) => {
return (
<SafeAreaView style={{flex: 1}}>
<View style={{flex: 1, padding: 16}}>
<View style={styles.container}>
<Text style={styles.header}>
You are on Setting Screen
</Text>
<TouchableOpacity
style={styles.buttonStyle}
onPress={() => navigation.navigate('Home')}>
<Text>Go to Home Tab</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.buttonStyle}
onPress={() => navigation.navigate('Details')}>
<Text>Open Detail Screen</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.buttonStyle}
onPress={() => navigation.navigate('Profile')}>
<Text>Open Profile Screen</Text>
</TouchableOpacity>
</View>
<Text style={styles.footerHeading}>
Bottom Navigation with Navigation Icon from Local Directory
</Text>
<Text style={styles.footerText}>www.aboutreact.com</Text>
</View>
</SafeAreaView>
);
};
export default SettingsScreen;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
header: {
fontSize: 25,
textAlign: 'center',
marginBottom: 16,
},
footerHeading: {
fontSize: 18,
textAlign: 'center',
color: 'grey',
},
footerText: {
fontSize: 16,
textAlign: 'center',
color: 'grey',
},
buttonStyle: {
alignItems: 'center',
backgroundColor: '#DDDDDD',
padding: 10,
width: 300,
marginTop: 16,
},
});
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
That was the React Native Bottom Navigation. 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. 🙂
Hello, how can to hide the navbar in some screnn?
Please have a look at the expo https://snack.expo.io/@aboutreact/hide-bottom-navigation-in-some-screens
sir, if i want to use state in App.js class so how do it..
please tell..
thanks…
You have to use Redux for that
This one finally worked!!! Thank you so much guys