Creating a Custom Tab Bar for Tab Navigation in Expo React Native
React Native|OCTOBER 14, 2024|70 VIEWS
Tabs in Expo Router provide a simple UI for tab navigation, but sometimes it may not meet all your needs. For instance, you might want to create a custom tab bar to add features like displaying a message count on the messages tab.
In this tutorial, we will walk through the process of creating a custom tab bar for tab navigation in an Expo React Native application. By the end of this guide, you will have a fully functional custom tab bar that enhances the user experience of your app. Let's get started!
Create CustomTabBar component
// CustomTabBar.tsx
import { BottomTabBarProps } from "@react-navigation/bottom-tabs";
import React from "react";
import { StyleSheet, Text, TouchableOpacity, View } from "react-native";
export default function CustomTabBar({
state,
descriptors,
navigation,
}: BottomTabBarProps) {
return (
<View style={styles.tabBar}>
{state.routes.map((route, index) => {
const { options } = descriptors[route.key];
const label = options.title !== undefined ? options.title : route.name;
const isFocused = state.index === index;
const onPress = () => {
const event = navigation.emit({
type: "tabPress",
target: route.key,
canPreventDefault: true,
});
if (!isFocused && !event.defaultPrevented) {
navigation.navigate(route.name);
}
};
const onLongPress = () => {
navigation.emit({
type: "tabLongPress",
target: route.key,
});
};
return (
<TouchableOpacity
key={index}
accessibilityRole="button"
accessibilityState={isFocused ? { selected: true } : {}}
accessibilityLabel={options.tabBarAccessibilityLabel}
testID={options.tabBarTestID}
onPress={onPress}
onLongPress={onLongPress}
style={styles.tab}
>
{options.tabBarIcon &&
options.tabBarIcon({
focused: isFocused,
color: isFocused ? "#673ab7" : "#222",
size: 24,
})}
<Text style={{ color: isFocused ? "#673ab7" : "#222" }}>
{label}
</Text>
</TouchableOpacity>
);
})}
</View>
);
}
const styles = StyleSheet.create({
tabBar: {
flexDirection: "row",
height: 60,
backgroundColor: "#fff",
},
tab: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
screen: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
});
Now you have full control over the tab bar and can customize it to suit your needs.
Integrate the Custom Tab Bar
Now that you have created the CustomTabBar
component, it's time to integrate it into your application. Here's how you can do it:
import { Tabs } from "expo-router";
import React from "react";
import { TabBarIcon } from "@/components/navigation/TabBarIcon";
import CustomTabBar from "@/components/CustomTabBar";
export default function TabLayout() {
return (
<Tabs
tabBar={(props) => <CustomTabBar {...props} />}
screenOptions={{
headerShown: false,
}}
>
<Tabs.Screen
name="index"
options={{
title: "Home",
tabBarIcon: ({ color, focused }) => (
<TabBarIcon
name={focused ? "home" : "home-outline"}
color={color}
/>
),
}}
/>
<Tabs.Screen
name="explore"
options={{
title: "Explore",
tabBarIcon: ({ color, focused }) => (
<TabBarIcon
name={focused ? "code-slash" : "code-slash-outline"}
color={color}
/>
),
}}
/>
</Tabs>
);
}