What is @react-navigation/material-top-tabs?
@react-navigation/material-top-tabs is a library for React Native that provides a material-design styled top tab navigator. It allows you to create a tab-based navigation structure where the tabs are displayed at the top of the screen, similar to the tab layout in many popular mobile applications.
What are @react-navigation/material-top-tabs's main functionalities?
Basic Top Tab Navigator
This code demonstrates how to set up a basic top tab navigator with two tabs: Home and Settings. Each tab is associated with a screen component.
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
import HomeScreen from './HomeScreen';
import SettingsScreen from './SettingsScreen';
const Tab = createMaterialTopTabNavigator();
function MyTabs() {
return (
<Tab.Navigator>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Settings" component={SettingsScreen} />
</Tab.Navigator>
);
}
export default function App() {
return (
<NavigationContainer>
<MyTabs />
</NavigationContainer>
);
}
Customizing Tab Bar Options
This code shows how to customize the tab bar options, such as changing the active tint color, label style, and background color of the tab bar.
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
import HomeScreen from './HomeScreen';
import SettingsScreen from './SettingsScreen';
const Tab = createMaterialTopTabNavigator();
function MyTabs() {
return (
<Tab.Navigator
tabBarOptions={{
activeTintColor: 'white',
labelStyle: { fontSize: 12 },
style: { backgroundColor: 'blue' },
}}
>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Settings" component={SettingsScreen} />
</Tab.Navigator>
);
}
export default function App() {
return (
<NavigationContainer>
<MyTabs />
</NavigationContainer>
);
}
Lazy Loading Tabs
This code demonstrates how to enable lazy loading for the tabs. With lazy loading, the tab screens are not mounted until they are first focused.
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
import HomeScreen from './HomeScreen';
import SettingsScreen from './SettingsScreen';
const Tab = createMaterialTopTabNavigator();
function MyTabs() {
return (
<Tab.Navigator lazy={true}>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Settings" component={SettingsScreen} />
</Tab.Navigator>
);
}
export default function App() {
return (
<NavigationContainer>
<MyTabs />
</NavigationContainer>
);
}
Other packages similar to @react-navigation/material-top-tabs
@react-navigation/bottom-tabs
@react-navigation/bottom-tabs provides a bottom tab navigator, which is similar to the top tab navigator but places the tabs at the bottom of the screen. It is useful for applications that prefer bottom navigation, which is common in many mobile apps.
react-native-tab-view
react-native-tab-view is another library for creating tab-based navigation in React Native. It provides a customizable tab view component that can be used to create both top and bottom tab navigators. It offers more flexibility in terms of customization compared to @react-navigation/material-top-tabs.
react-navigation-tabs
react-navigation-tabs is a part of the React Navigation library that provides both top and bottom tab navigators. It is a more general-purpose tab navigation solution compared to @react-navigation/material-top-tabs, which is specifically focused on material design top tabs.