React-Native Tab Navigation
Syntax
Tab Navigation in React Native can be implemented using the createBottomTabNavigator
function from the React Navigation library.
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
const Tab = createBottomTabNavigator();
function App() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Settings" component={SettingsScreen} />
</Tab.Navigator>
);
}
Example
import React from 'react';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { NavigationContainer } from '@react-navigation/native';
import Icon from 'react-native-vector-icons/Ionicons';
import HomeScreen from './screens/HomeScreen';
import SettingsScreen from './screens/SettingsScreen';
const Tab = createBottomTabNavigator();
export default function App() {
return (
<NavigationContainer>
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName;
if (route.name === 'Home') {
iconName = focused ? 'home' : 'home-outline';
} else if (route.name === 'Settings') {
iconName = focused ? 'settings' : 'settings-outline';
}
return <Icon name={iconName} size={size} color={color} />;
},
})}
tabBarOptions={{
activeTintColor: 'tomato',
inactiveTintColor: 'gray',
}}
>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Settings" component={SettingsScreen} />
</Tab.Navigator>
</NavigationContainer>
);
}
Output
The example above will render a bottom tab navigation bar with two tabs: Home and Settings. The active tab will be highlighted with the color tomato
.
Explanation
Tab Navigation is a popular UI component in mobile applications and can be easily implemented in React Native using the createBottomTabNavigator
function from the React Navigation library.
The createBottomTabNavigator
function allows for the creation of multiple screens that can be navigated using a tab bar at the bottom of the screen.
Use
Tab Navigation can be used in any React Native application where a user needs to access multiple screens easily.
To use Tab Navigation:
- Install the
@react-navigation/bottom-tabs
package using npm or yarn. - Import the necessary modules from the React Navigation library.
- Create a navigator using the
createBottomTabNavigator
function. - Define the screens and their components.
- Customize the tab bar appearance and behavior using
tabBarOptions
andscreenOptions
.
Important Points
- Tab Navigation is a popular UI component in mobile applications.
- The React Navigation library provides a convenient API for implementing Tab Navigation in React Native applications.
- The appearance and behavior of the tab bar can be customized using the
tabBarOptions
andscreenOptions
props.
Summary
Tab Navigation is an essential UI component in mobile applications, and React Native provides an easy way to implement it using the createBottomTabNavigator
function from the React Navigation library. With customizable options for appearance and behavior, developers can create a user-friendly and intuitive app experience.