import React, { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
export default function CustomTabBarScreen() {
const [selectedTab, setSelectedTab] = useState('Home');
const tabs = [
{ key: 'Home', label: 'Home', icon: '🏠' },
{ key: 'Search', label: 'Search', icon: '🔍' },
{ key: 'Profile', label: 'Profile', icon: '👤' }
];
return (
<View style={styles.container}>
<View style={styles.content}>
<Text style={styles.contentText}>You are on the {selectedTab} tab</Text>
</View>
<View style={styles.tabBar}>
{tabs.map(tab => {
const isSelected = selectedTab === tab.key;
return (
<TouchableOpacity
key={tab.key}
style={styles.tabButton}
onPress={() => setSelectedTab(tab.key)}
accessibilityRole="button"
accessibilityState={{ selected: isSelected }}
accessibilityLabel={`${tab.label} tab`}
>
<Text style={[styles.icon, isSelected && styles.selected]}>{tab.icon}</Text>
<Text style={[styles.label, isSelected && styles.selected]}>{tab.label}</Text>
</TouchableOpacity>
);
})}
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'space-between',
paddingBottom: 20,
backgroundColor: '#fff'
},
content: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
contentText: {
fontSize: 24,
color: '#333'
},
tabBar: {
flexDirection: 'row',
justifyContent: 'space-around',
borderTopWidth: 1,
borderTopColor: '#ccc',
paddingVertical: 10,
backgroundColor: '#f9f9f9'
},
tabButton: {
alignItems: 'center'
},
icon: {
fontSize: 24,
color: '#666'
},
label: {
fontSize: 12,
color: '#666',
marginTop: 4
},
selected: {
color: '#007AFF',
fontWeight: '600'
}
});This solution uses React Native's useState hook to keep track of the selected tab. We define an array of tabs with keys, labels, and emoji icons for simplicity.
The content area shows a text that updates based on the selected tab.
The custom tab bar is a View with three TouchableOpacity buttons. Each button shows an icon and label. When a tab is selected, its icon and label change color to blue (#007AFF) and become bold to highlight it.
Accessibility roles and states are added for better screen reader support.
This approach is simple and clear for beginners to understand how to build a custom tab bar without external libraries.