Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 3 of 10
Bottom Tab Navigator
Bottom tabs are the primary navigation pattern for most mobile apps. Users expect tab switching to be instant with the screen state preserved (not remounted). React Navigation's Tab navigator achieves this with lazy mounting.
Tab Navigator with Icons and Badges
npm install @react-navigation/bottom-tabs @expo/vector-iconsProduction Tab Setup
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { Ionicons } from '@expo/vector-icons';
export type MainTabParamList = {
Home: undefined;
Search: undefined;
Notifications: { initialTab?: 'all' | 'mentions' };
Profile: { userId: string };
};
const Tab = createBottomTabNavigator<MainTabParamList>();
function MainTabs() {
const [notificationCount, setNotificationCount] = React.useState(3);
return (
<Tab.Navigator
screenOptions={({ route }) => ({
headerShown: false, // hide default Tab header; Stack handles headers
tabBarActiveTintColor: '#229c6b',
tabBarInactiveTintColor: '#64748b',
tabBarStyle: {
backgroundColor: '#0f172a',
borderTopColor: '#1e293b',
borderTopWidth: 1,
height: 84,
paddingBottom: 28, // leave room for home indicator
paddingTop: 8,
},
tabBarLabelStyle: { fontSize: 11, fontWeight: '600' },
tabBarIcon: ({ color, size, focused }) => {
const icons: Record<string, keyof typeof Ionicons.glyphMap> = {
Home: focused ? 'home' : 'home-outline',
Search: focused ? 'search' : 'search-outline',
Notifications: focused ? 'notifications' : 'notifications-outline',
Profile: focused ? 'person' : 'person-outline',
};
return <Ionicons name={icons[route.name]} size={size} color={color} />;
},
})}
>
<Tab.Screen name="Home" component={HomeStack} />
<Tab.Screen name="Search" component={SearchStack} />
<Tab.Screen
name="Notifications"
component={NotificationsStack}
options={{
tabBarBadge: notificationCount > 0 ? notificationCount : undefined,
tabBarBadgeStyle: { backgroundColor: '#ef4444', fontSize: 10 },
}}
/>
<Tab.Screen name="Profile" component={ProfileStack}
initialParams={{ userId: 'me' }} // default param
/>
</Tab.Navigator>
);
}
// ---- Tab state preservation (screens mount once, not remounted on tab switch) ----
// By default: tab screens are lazily mounted (first visit) and kept mounted thereafter
// This means useEffect runs once, state is preserved on tab switch
// Force remount on tab focus (rare - usually wrong):
import { useFocusEffect } from '@react-navigation/native';
function SearchScreen() {
useFocusEffect(
React.useCallback(() => {
// Runs when screen comes into focus (tab selected)
return () => {
// Runs when screen loses focus (another tab selected)
};
}, [])
);
}Tip
Tip
Practice Bottom Tab Navigator in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Persistent tab navigation with badge counts and screen focus tracking
Practice Task
Note
Practice Task - (1) Write a working example of Bottom Tab Navigator from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Bottom Tab Navigator is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?