RN Lez 26: React Navigation — Tab
Docs: Navigation, React Navigation tabs docs
Obiettivi
Section titled “Obiettivi”- Creare Bottom Tab navigator
- Aggiungere icone con @expo/vector-icons
- Combinare Tab + Stack
1. Installazione
Section titled “1. Installazione”npm install @react-navigation/bottom-tabsnpx expo install @expo/vector-icons2. Tab Navigator base
Section titled “2. Tab Navigator base”import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';import { Ionicons } from '@expo/vector-icons';
type TabParamList = { Home: undefined; Cerca: undefined; Profilo: undefined;};
const Tab = createBottomTabNavigator<TabParamList>();
export default function App() { return ( <NavigationContainer> <Tab.Navigator screenOptions={({ route }) => ({ tabBarIcon: ({ focused, color, size }) => { let iconName: keyof typeof Ionicons.glyphMap; if (route.name === 'Home') iconName = focused ? 'home' : 'home-outline'; else if (route.name === 'Cerca') iconName = focused ? 'search' : 'search-outline'; else iconName = focused ? 'person' : 'person-outline'; return <Ionicons name={iconName} size={size} color={color} />; }, tabBarActiveTintColor: '#3498db', tabBarInactiveTintColor: 'grey', })} > <Tab.Screen name="Home" component={HomeScreen} options={{ title: 'Home' }} /> <Tab.Screen name="Cerca" component={CercaScreen} options={{ title: 'Cerca' }} /> <Tab.Screen name="Profilo" component={ProfiloScreen} options={{ title: 'Profilo' }} /> </Tab.Navigator> </NavigationContainer> );}3. Tab con badge
Section titled “3. Tab con badge”<Tab.Screen name="Notifiche" component={NotificheScreen} options={{ tabBarBadge: 3, // ← mostra "3" sul tab }}/>4. Combinazione Tab + Stack
Section titled “4. Combinazione Tab + Stack”// HomeStack.js — Stack dentro il tab Homeconst HomeStack = createNativeStackNavigator();
function HomeStackScreen() { return ( <HomeStack.Navigator> <HomeStack.Screen name="Feed" component={FeedScreen} /> <HomeStack.Screen name="Post" component={PostScreen} /> </HomeStack.Navigator> );}
// App.js — Tab Navigator con Stack dentro<Tab.Screen name="Home" component={HomeStackScreen} />5. Tab personalizzato
Section titled “5. Tab personalizzato”<Tab.Navigator screenOptions={{ tabBarStyle: { backgroundColor: 'white', borderTopWidth: 1, borderTopColor: '#eee', height: 60, paddingBottom: 8, }, tabBarLabelStyle: { fontSize: 12, fontWeight: '600', }, }}>6. Tabella riassuntiva
Section titled “6. Tabella riassuntiva”| Componente | Codice |
|---|---|
| Tab Navigator | createBottomTabNavigator() |
| Icone | @expo/vector-icons (Ionicons) |
| Badge | tabBarBadge: numero |
| Stack + Tab | Tab contiene Stack al suo interno |
7. Esercizio
Section titled “7. Esercizio”🎯 “App con 3 tab”
Section titled “🎯 “App con 3 tab””Crea un’app con:
- Tab Home: FlatList di notizie
- Tab Cerca: TextInput + risultati
- Tab Profilo: nome, email, foto
- Icone Ionicons (home, search, person)
- Tab attivo: blu, inattivo: grigio
- Tab Home contiene Stack (Feed → Dettaglio notizia)