Updated 22 November 2024
In this blog, we will learn about Navigation and Bottom tab navigation in react native.
Before going to our topic Bottom tab navigation just take a look at how navigation works in react native.
As we know a website or a mobile application consists of many screens and pages. To move from one screen to another or to show another screen on a button click is navigation.
For example, open a login screen on the login button click.
On the web, we use a href tag.
1 |
<a href="About.html">About Us</a> |
Navigation in react native can be achieved using the navigation library and navigate prop.
In this example, we have two screens Home screen and About screen.
The home screen has a button on which click you will navigate to the “About screen”.
1 2 3 |
npm install @react-navigation/stack npm install @react-navigation/native |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import React from "react"; import { Button, View, Text } from "react-native"; import React from "react"; import { Button, View, Text } from "react-native"; export default class Home extends React.Component{ render(){ return( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text> This is Home Screen </Text> <Button title="Go to About" onPress={() => this.props.navigation.navigate('about')} /> </View> ) } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
import React, { Component } from 'react'; import { View, Text } from 'react-native'; export default class Aboutscreen extends Component { render() { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text>About Screen</Text> </View> ) } } |
Now main part of our sample code due to which we will be able to navigate is in our App.js file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import React from 'react'; import { StyleSheet } from 'react-native'; import {createAppContainer } from "react-navigation"; import { createStackNavigator } from 'react-navigation-stack'; import Home from './Home'; import About from'./About'; export default class App extends React.Component { render() { return <AppContainer />; } } const AppNavigator = createStackNavigator({ Home:{ screen : Home }, about:{ screen: About } },{ initialRouteName: "Home" }); const AppContainer = createAppContainer(AppNavigator) const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, }); |
Now Run the sample App.
So far, we have learned how navigation works in react native. Now our sample app is able to navigate from one screen to another.
So, let’s move to the Bottom Tab Navigation.
Bottom tab navigation is one of the most used navigation in mobile apps.
1. Bottom tab Navigation- In the bottom tab navigation tab is aligned to the bottom of the screen
2. Top tab navigation- Tabs are aligned to the top of the screen below the header or toolbar.
Let’s take an example of the bottom tab bar navigation
For the bottom tab bar, we are reusing the above navigation example.
1 2 |
npm install @react-navigation/bottom-tabs // for bottom tab bar npm install --save react-native-vector-icons // for icons on bottom tab bar |
For the Home screen and About screen we used the same code as in the navigation example. If you want to add or modify or redesign your screen you can do it accordingly.
There is little change in the app.js class for bottom tab navigation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import React from 'react'; import Home from './Home'; import About from'./About'; import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; import { NavigationContainer} from '@react-navigation/native'; import Ionicons from 'react-native-vector-icons/Ionicons'; const sampleTabNavigation = createBottomTabNavigator( ); export default class App extends React.Component { render() { return ( <NavigationContainer> <sampleTabNavigation.Navigator screenOptions={({ route }) => ({ tabBarIcon: ({ focused, color, size }) => { let iconName; if (route.name === 'Home') { iconName = 'md-home-sharp'; } else if (route.name === 'about') { iconName = 'md-reader-outline'; } return <Ionicons name = {iconName} size={size} color={color} />; }, })} tabBarOptions={{ activeTintColor: 'red', inactiveTintColor: 'gray', }} > <sampleTabNavigation.Screen name="Home" component={Home} /> <sampleTabNavigation.Screen name="about" component={About} /> </sampleTabNavigation.Navigator> </NavigationContainer> ); } } |
Now run the sample code
You may face some issues/errors related to gesture and surface view and view area and screen.
To resolve these errors you have to add the below dependency to your project.
1 2 3 4 5 6 7 8 9 |
npm install react-native-gesture-handler npm install react-native-screens npm install react-native-safe-area-context npm install react-native-safe-area-view npm install --save @react-native-masked-view/masked-view |
If you want to learn more about bottom tab navigation in react native, you can visit here.
In this blog, we learned about Bottom Tab Navigation in React Native and explored how to set up a navigation flow between multiple screens using the @react-navigation/bottom-tabs
library.
By implementing bottom tab navigation, we can create a more intuitive and accessible UI for users to switch between different sections of an app seamlessly.
We also covered how to enhance the navigation experience by using custom icons and configuring the tab bar’s appearance.
You can also check other blogs from here.
Thank you for reading! ❤️
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.