react navigation中點擊底部tab怎么傳遞參數(shù)
可以通過在底部tab的onPress事件中調(diào)用navigation.navigate方法,并在第二個參數(shù)中傳遞參數(shù)來實現(xiàn)傳遞參數(shù)。
例如:
<Tab.Screen
name="Home"
component={HomeScreen}
options={({ route, navigation }) =>({
tabBarButton: (props) => (
<TouchableOpacity
{...props}
onPress={() => {
console.log(props)
console.log(navigation)
// 傳遞參數(shù)
navigation.navigate('掃一掃', { page: 'aaa' });
}}
/>
),
})}
/>在HomeScreen組件中可以通過route.params獲取傳遞的參數(shù)。
例如:
function HomeScreen({ route }) {
const { param1, param2 } = route.params;
// 使用傳遞的參數(shù)
return (
<View>
<Text>{param1}</Text>
<Text>{param2}</Text>
</View>
);
}
Tab.Navigator 配置
Tab.Navigator是React Navigation中用于創(chuàng)建底部導航欄的組件,它可以通過一些配置來自定義底部導航欄的樣式和行為。
以下是一些常用的Tab.Navigator配置:
- initialRouteName:指定初始路由名稱。
- tabBarOptions:配置底部導航欄的樣式和行為,例如顏色、圖標、標簽等。
- screenOptions:配置每個Tab.Screen的默認選項,例如標題、圖標等。
- tabBarIcon:自定義底部導航欄圖標的組件。
- tabBarLabel:自定義底部導航欄標簽的組件。
以下是一個示例代碼:
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { MaterialCommunityIcons } from '@expo/vector-icons';
const Tab = createBottomTabNavigator();
function MyTabs() {
? return (
? ? <Tab.Navigator
? ? ? initialRouteName="Home"
? ? ? tabBarOptions={{
? ? ? ? activeTintColor: '#e91e63',
? ? ? ? inactiveTintColor: '#888',
? ? ? }}
? ? ? screenOptions={({ route }) => ({
? ? ? ? tabBarIcon: ({ focused, color, size }) => {
? ? ? ? ? let iconName;
? ? ? ? ? if (route.name === 'Home') {
? ? ? ? ? ? iconName = focused
? ? ? ? ? ? ? ? 'home'
? ? ? ? ? ? ? : 'home-outline';
? ? ? ? ? } else if (route.name === 'Settings') {
? ? ? ? ? ? iconName = focused ? 'settings' : 'settings-outline';
? ? ? ? ? }
? ? ? ? ? // You can return any component that you like here!
? ? ? ? ? return <MaterialCommunityIcons name={iconName} size={size} color={color} />;
? ? ? ? },
? ? ? })}
? ? >
? ? ? <Tab.Screen
? ? ? ? name="Home"
? ? ? ? component={HomeScreen}
? ? ? ? options={{
? ? ? ? ? tabBarLabel: 'Home',
? ? ? ? }}
? ? ? />
? ? ? <Tab.Screen
? ? ? ? name="Settings"
? ? ? ? component={SettingsScreen}
? ? ? ? options={{
? ? ? ? ? tabBarLabel: 'Settings',
? ? ? ? }}
? ? ? />
? ? </Tab.Navigator>
? );
}在這個示例中,我們使用了MaterialCommunityIcons組件來自定義底部導航欄的圖標,使用了activeTintColor和inactiveTintColor來配置選中和未選中狀態(tài)下的顏色,使用了screenOptions來配置每個Tab.Screen的默認選項。
到此這篇關于react navigation中點擊底部tab怎么傳遞參數(shù)的文章就介紹到這了,更多相關react navigation tab傳遞參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
react?hooks?UI與業(yè)務邏輯分離必要性技術方案
這篇文章主要為大家介紹了react?hooks?UI與業(yè)務邏輯分離必要性技術方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
React?hook實現(xiàn)簡單的websocket封裝方式
這篇文章主要介紹了React?hook實現(xiàn)簡單的websocket封裝方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
react組件中的constructor和super知識點整理
這篇文章主要介紹了react組件中的constructor和super知識點整理,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
React避坑指南之useEffect 依賴引用類型問題分析
這篇文章主要介紹了React避坑指南之useEffect 依賴引用類型問題分析,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
React Hook父組件如何獲取子組件的數(shù)據(jù)/函數(shù)
這篇文章主要介紹了React Hook父組件如何獲取子組件的數(shù)據(jù)/函數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

