Design Converter
Education
Developer Advocate
Last updated on Oct 27, 2023
Last updated on Sep 25, 2023
React Navigation is a popular library in the React Native ecosystem that provides a way for your app to transition between screens. One of the most common patterns in mobile applications is the use of a bottom tab bar to switch between different sections of an app. React Navigation provides a bottom tab navigator to implement this pattern in your React Native app.
React Navigation bottom tabs are a type of navigation component that allows users to navigate between different screens or sections of an app by tapping on tabs in a bar at the bottom of the screen. This pattern is widely used in many popular apps, such as Instagram, Facebook, and Twitter.
The bottom navigation component in React is a part of the React Navigation library. It provides a way to create a tab-based navigation layout with a bar at the bottom of the screen. This bar typically contains several tabs, each representing a different section of the app. When a user taps on a tab, the app navigates to the corresponding screen.
The bottom navigation component is highly customizable. You can customize the appearance of the tab bar, the tab icons, and the active tab indicator. You can also control the behavior of the tab navigation, such as the initial screen to display and the screen to navigate to when a tab is tapped.
Before you can start using React Navigation bottom tabs, you need to set up a React Native project. If you haven't already, you can create a new React Native project by running the following command in your terminal:
1 npx react-native init MyProject 2
After creating your project, you need to install the React Navigation library. You can do this by running the following command in your project directory:
1 npm install @react-navigation/native 2
This will install the core dependencies needed for navigation in your React Native project.
After setting up your project, you can start creating the screens for your app. In a typical React Native app, each screen is represented by a component. For example, you might have a HomeScreen component for the home screen, a SettingsScreen component for the settings screen, and so on.
To create a screen, you can define a new function component. Here's an example of a simple HomeScreen component:
1 import React from 'react'; 2 import { View, Text, StyleSheet } from 'react-native'; 3 4 export default function HomeScreen() { 5 return ( 6 <View style={styles.container}> 7 <Text>Home Screen</Text> 8 </View> 9 ); 10 } 11 12 const styles = StyleSheet.create({ 13 container: { 14 flex: 1, 15 justifyContent: 'center', 16 alignItems: 'center', 17 }, 18 }); 19
In this example, the HomeScreen component is a simple view with a text element that displays "Home Screen".
Once you have created the screens for your app, you can start implementing the bottom tab navigator. The bottom tab navigator is a component provided by the React Navigation library that allows you to create a tab-based navigation layout.
To use the bottom tab navigator, you first need to install the @react-navigation/bottom-tabs package. You can do this by running the following command in your project directory:
1 npm install @react-navigation/bottom-tabs 2
After installing the package, you can import the createBottomTabNavigator function from @react-navigation/bottom-tabs and use it to create a tab navigator. Here's an example:
1 import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; 2 3 const Tab = createBottomTabNavigator(); 4 5 export default function App() { 6 return ( 7 <Tab.Navigator> 8 <Tab.Screen name="Home" component={HomeScreen} /> 9 <Tab.Screen name="Settings" component={SettingsScreen} /> 10 </Tab.Navigator> 11 ); 12 } 13
In this example, the createBottomTabNavigator function is used to create a Tab component. The Tab.Navigator component is then used to define the tab navigation structure. Each Tab.Screen component represents a screen in the tab navigation.
The tab bar in React Navigation is highly customizable. You can customize the appearance of the tab bar, the tab icons, and the active tab indicator.
To customize the tab bar, you can use the tabBarOptions prop of the Tab.Navigator component. This prop accepts an object that specifies various styling options for the tab bar.
Here's an example of how you can customize the tab bar:
1 <Tab.Navigator 2 tabBarOptions={{ 3 activeTintColor: 'tomato', 4 inactiveTintColor: 'gray', 5 }} 6 > 7 {/* ... */} 8 </Tab.Navigator> 9
In this example, the activeTintColor option is used to change the color of the active tab to tomato, and the inactiveTintColor option is used to change the color of the inactive tabs to gray.
In addition to customizing the appearance of the tab bar, you can also add icons to the tabs. To do this, you can use the tabBarIcon prop of the Tab.Screen component.
The tabBarIcon prop accepts a function that returns a React component to display as the tab icon. This function is called with an object that contains the focused state, the color, and the size to use for the icon.
Here's an example of how you can add icons to the tabs:
1 import { Ionicons } from '@expo/vector-icons'; 2 3 <Tab.Screen 4 name="Home" 5 component={HomeScreen} 6 options={{ 7 tabBarIcon: ({ focused, color, size }) => ( 8 <Ionicons 9 name={focused ? 'home' : 'home-outline'} 10 size={size} 11 color={color} 12 /> 13 ), 14 }} 15 /> 16
In this example, the Ionicons component from the @expo/vector-icons package is used to display the tab icon. The name prop of the Ionicons component is used to change the icon depending on whether the tab is focused.
React Navigation provides a simple and intuitive API for navigating between tabs. When a user taps on a tab in the tab bar, the app automatically navigates to the corresponding screen.
You can also programmatically navigate between tabs using the navigation.navigate function. This function accepts the name of the screen to navigate to as its argument.
Here's an example of how you can navigate to the Settings screen from the Home screen:
1 import { Button } from 'react-native'; 2 3 export default function HomeScreen({ navigation }) { 4 return ( 5 <View style={styles.container}> 6 <Text>Home Screen</Text> 7 <Button 8 title="Go to Settings" 9 onPress={() => navigation.navigate('Settings')} 10 /> 11 </View> 12 ); 13 } 14
In this example, a button is added to the Home screen. When the button is pressed, the app navigates to the Settings screen.
The tab navigator in React Navigation is a powerful tool for creating tab-based navigation layouts. It allows you to define a set of screens and switch between them by tapping on tabs in a bar at the bottom of the screen.
Each screen in the tab navigator is represented by a Tab.Screen component. The name prop of the Tab.Screen component is used to identify the screen, and the component prop is used to specify the component to render for the screen.
Here's an example of how you can use the tab navigator to switch between the Home screen and the Settings screen:
1 <Tab.Navigator> 2 <Tab.Screen name="Home" component={HomeScreen} /> 3 <Tab.Screen name="Settings" component={SettingsScreen} /> 4 </Tab.Navigator> 5
In this example, the Tab.Navigator component is used to define the tab navigation structure. The Tab.Screen components represent the Home screen and the Settings screen.
By default, the first screen in the tab navigator is displayed as the initial screen when the app starts. However, you can change this by using the initialRouteName prop of the Tab.Navigator component.
The initialRouteName prop accepts the name of the screen to display as the initial screen. Here's an example of how you can set the Settings screen as the initial screen:
1 <Tab.Navigator initialRouteName="Settings"> 2 <Tab.Screen name="Home" component={HomeScreen} /> 3 <Tab.Screen name="Settings" component={SettingsScreen} /> 4 </Tab.Navigator> 5
In this example, the initialRouteName prop is set to "Settings", so the Settings screen is displayed as the initial screen when the app starts.
The active tab in the bottom tab bar is the tab corresponding to the currently displayed screen. By default, the active tab is highlighted with a different color to distinguish it from the other tabs.
You can customize the appearance of the active tab using the tabBarOptions prop of the Tab.Navigator component. This prop accepts an object that specifies various styling options for the tab bar.
Here's an example of how you can customize the active tab:
1 <Tab.Navigator 2 tabBarOptions={{ 3 activeTintColor: 'tomato', 4 inactiveTintColor: 'gray', 5 }} 6 > 7 {/* ... */} 8 </Tab.Navigator> 9
In this example, the activeTintColor option is used to change the color of the active tab to tomato, and the inactiveTintColor option is used to change the color of the inactive tabs to gray.
In a typical social media app, users can view and create posts. You can add a Posts screen to your app to allow users to view posts.
To create a Posts screen, you can define a new function component. Here's an example of a simple PostsScreen component:
1 import React from 'react'; 2 import { View, Text, StyleSheet } from 'react-native'; 3 4 export default function PostsScreen() { 5 return ( 6 <View style={styles.container}> 7 <Text>Posts Screen</Text> 8 </View> 9 ); 10 } 11 12 const styles = StyleSheet.create({ 13 container: { 14 flex: 1, 15 justifyContent: 'center', 16 alignItems: 'center', 17 }, 18 }); 19
In this example, the PostsScreen component is a simple view with a text element that displays "Posts Screen". You can add this screen to your tab navigator to allow users to navigate to it.
In many apps, you might want to use a stack navigator in combination with a bottom tab navigator. This allows you to have a stack of screens for each tab, where the user can navigate forward and backward within each tab.
To use a stack navigator with a bottom tab navigator, you first need to install the @react-navigation/stack package. You can do this by running the following command in your project directory:
1 npm install @react-navigation/stack 2
After installing the package, you can import the createStackNavigator function from @react-navigation/stack and use it to create a stack navigator. You can then use this stack navigator as a screen in your bottom tab navigator.
React Navigation allows you to nest navigators to create complex navigation structures. This means you can have a navigator inside another navigator.
For example, you can have a stack navigator inside a bottom tab navigator. This allows you to have a stack of screens for each tab, where the user can navigate forward and backward within each tab.
Here's an example of how you can nest a stack navigator inside a bottom tab navigator:
1 import { createStackNavigator } from '@react-navigation/stack'; 2 3 const HomeStack = createStackNavigator(); 4 5 function HomeStackScreen() { 6 return ( 7 <HomeStack.Navigator> 8 <HomeStack.Screen name="Home" component={HomeScreen} /> 9 <HomeStack.Screen name="Details" component={DetailsScreen} /> 10 </HomeStack.Navigator> 11 ); 12 } 13 14 <Tab.Navigator> 15 <Tab.Screen name="Home" component={HomeStackScreen} /> 16 {/* ... */} 17 </Tab.Navigator> 18
In this example, a HomeStack navigator is created using the createStackNavigator function. This navigator is then used as a screen in the Tab.Navigator component. This allows the user to navigate between the Home screen and the Details screen within the Home tab.
In a React Native app, the back button press behavior can be customized. By default, when the user presses the back button, the app navigates back to the previous screen in the navigation stack. If there are no screens to go back to, the app exits.
However, you can override this default behavior by using the backBehavior prop of the Tab.Navigator component. This prop accepts one of the following values: 'initialRoute', 'order', 'history', or 'none'.
Here's an example of how you can set the back button behavior to navigate to the initial route:
1 <Tab.Navigator backBehavior="initialRoute"> 2 {/* ... */} 3 </Tab.Navigator> 4
In this example, the backBehavior prop is set to 'initialRoute', so when the user presses the back button, the app navigates to the initial route in the tab navigator.
In addition to the tab bar, you can also customize the navigation bar in your React Native app. The navigation bar is the bar at the top of the screen that typically contains the title of the current screen and navigation controls.
To customize the navigation bar, you can use the screenOptions prop of the StackNavigator component. This prop accepts an object that specifies various styling and behavior options for the screens in the stack navigator.
Here's an example of how you can customize the navigation bar:
1 <HomeStack.Navigator 2 screenOptions={{ 3 headerStyle: { 4 backgroundColor: '#f4511e', 5 }, 6 headerTintColor: '#fff', 7 headerTitleStyle: { 8 fontWeight: 'bold', 9 }, 10 }} 11 > 12 {/* ... */} 13 </HomeStack.Navigator> 14
In this example, the headerStyle option is used to change the background color of the navigation bar to orange, the headerTintColor option is used to change the color of the title and buttons in the navigation bar to white, and the headerTitleStyle option is used to change the font weight of the title to bold.
React Native Paper is a collection of customizable and production-ready components for React Native, following Google’s Material Design guidelines. One of the components that React Native Paper provides is a bottom navigation component.
To use React Native Paper's bottom navigation, you first need to install the react-native-paper package. You can do this by running the following command in your project directory:
1 npm install react-native-paper 2
After installing the package, you can import the BottomNavigation component from react-native-paper and use it to create a bottom navigation layout. Here's an example:
1 import { BottomNavigation, Text } from 'react-native-paper'; 2 3 const HomeRoute = () => <Text>Home</Text>; 4 5 const SettingsRoute = () => <Text>Settings</Text>; 6 7 export default function App() { 8 const [index, setIndex] = React.useState(0); 9 const [routes] = React.useState([ 10 { key: 'home', title: 'Home', icon: 'home' }, 11 { key: 'settings', title: 'Settings', icon: 'settings' }, 12 ]); 13 14 const renderScene = BottomNavigation.SceneMap({ 15 home: HomeRoute, 16 settings: SettingsRoute, 17 }); 18 19 return ( 20 <BottomNavigation 21 navigationState={{ index, routes }} 22 onIndexChange={setIndex} 23 renderScene={renderScene} 24 /> 25 ); 26 } 27
In this example, the BottomNavigation component is used to create a bottom navigation layout with two tabs: Home and Settings. The navigationState prop is used to control the current index and routes, and the onIndexChange prop is used to handle index changes. The renderScene prop is used to render the scene for each route.
In some cases, you might want to override the default behavior of the tab bar button. For example, you might want to show a modal when a tab is pressed, instead of navigating to a screen.
You can override the default behavior of the tab bar button by using the tabBarButton prop of the Tab.Screen component. This prop accepts a function that returns a React component to use as the custom tab bar button.
Here's an example of how you can override the default behavior of the tab bar button:
1 import { Button } from 'react-native'; 2 3 <Tab.Screen 4 name="Home" 5 component={HomeScreen} 6 options={{ 7 tabBarButton: (props) => ( 8 <Button title="Custom Home Button" onPress={() => alert('Button pressed!')} /> 9 ), 10 }} 11 /> 12
In this example, the tabBarButton prop is used to render a custom button for the Home tab. When the button is pressed, an alert is shown instead of navigating to the Home screen.
Nested navigators allow you to create more complex navigation structures in your app. For example, you can have a stack navigator inside each tab of a bottom tab navigator. This allows you to have a stack of screens for each tab, where the user can navigate forward and backward within each tab.
Here's an example of how you can implement nested navigators in bottom tabs:
1 const HomeStack = createStackNavigator(); 2 3 function HomeStackScreen() { 4 return ( 5 <HomeStack.Navigator> 6 <HomeStack.Screen name="Home" component={HomeScreen} /> 7 <HomeStack.Screen name="Details" component={DetailsScreen} /> 8 </HomeStack.Navigator> 9 ); 10 } 11 12 <Tab.Navigator> 13 <Tab.Screen name="Home" component={HomeStackScreen} /> 14 {/* ... */} 15 </Tab.Navigator> 16
In this example, a HomeStack navigator is created using the createStackNavigator function. This navigator is then used as a screen in the Tab.Navigator component. This allows the user to navigate between the Home screen and the Details screen within the Home tab.
The screenOptions prop in React Navigation allows you to customize the appearance and behavior of all screens in a navigator. This prop accepts a function that returns an options object. The function is called with the route and navigation props of each screen.
Here's an example of how you can use the screenOptions prop to set the header title of each screen to its name:
1 <Tab.Navigator 2 screenOptions={({ route }) => ({ 3 headerTitle: route.name, 4 })} 5 > 6 {/* ... */} 7 </Tab.Navigator> 8
In this example, the screenOptions prop is used to set the headerTitle option to the name of the route. This changes the title of the header bar to the name of the current screen.
In a React Native app, the user plays a crucial role in navigation. The user can navigate between different screens by interacting with the navigation components, such as the tab bar and the navigation bar.
For example, when the user taps on a tab in the bottom tab bar, the app navigates to the corresponding screen. Similarly, when the user presses the back button in the navigation bar, the app navigates back to the previous screen in the navigation stack.
React Navigation provides a simple and intuitive API for handling user interactions. For example, you can use the navigation.navigate function to programmatically navigate to a specific screen, or the navigation.goBack function to navigate back to the previous screen.
While React Navigation is a powerful and flexible library, you might encounter some issues when using it. Here are some common issues and their solutions:
Implementing bottom navigation in React Native can greatly improve the user experience of your app. Here are some best practices to follow when implementing bottom navigation:
React Navigation bottom tabs are a powerful tool for creating tab-based navigation layouts in React Native apps. They provide a simple and intuitive way to navigate between different screens, and they can be customized to fit the look and feel of your app.
By understanding how to use React Navigation bottom tabs, you can create more user-friendly apps that provide a smooth and seamless navigation experience. Whether you're building a social media app, an e-commerce app, or any other type of app, React Navigation bottom tabs can help you create a better user experience.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.