Contents
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 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 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-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 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 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
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 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-native-gesture-handler';
import React from 'react';
import {Image} from 'react-native';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/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 = createStackNavigator();
const Tab = createBottomTabNavigator();
function HomeStack() {
return (
<Stack.Navigator
initialRouteName="Home"
screenOptions={{
headerStyle: {backgroundColor: '#42f44b'},
headerTintColor: '#fff',
headerTitleStyle: {fontWeight: 'bold'},
}}>
<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={{
headerStyle: {backgroundColor: '#42f44b'},
headerTintColor: '#fff',
headerTitleStyle: {fontWeight: 'bold'},
}}>
<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"
tabBarOptions={{
activeTintColor: '#42f44b',
}}>
<Tab.Screen
name="HomeStack"
component={HomeStack}
options={{
tabBarLabel: 'Home',
tabBarIcon: ({focused, color, size}) => (
<Image
source={
focused
? require('./asset/logo1.png')
: require('./asset/logo2.png')
}
style={{
width: size,
height: size,
borderRadius: size,
}}
/>
),
}}
/>
<Tab.Screen
name="SettingsStack"
component={SettingsStack}
options={{
tabBarLabel: 'Settings',
tabBarIcon: ({focused, color, size}) => (
<Image
source={
focused
? require('./asset/logo1.png')
: require('./asset/logo4.png')
}
style={{
width: size,
height: size,
borderRadius: size,
}}
/>
),
}}
/>
</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
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
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