gpt4 book ai didi

javascript - 嵌套在堆栈导航器中的 React Native 选项卡导航器

转载 作者:行者123 更新时间:2023-12-04 15:05:30 24 4
gpt4 key购买 nike

我是 React Native 的新手,并且在屏幕方面苦苦挣扎。我已经阅读了嵌套导航文档 ( https://reactnavigation.org/docs/nesting-navigators/ ),它帮助我设置了初始导航设置,但我遇到了一些问题。

我正在尝试设置应用程序,使其初始屏幕显示为“选择用户”,没有标签导航。选择用户后,您将被重定向到另一个具有选项卡导航的屏幕。我目前可以使用它,但是在初始屏幕后我无法访问任何路线/ Prop /参数。

我不得不使用 import { useNavigation } from "@react-navigation/native"; 手动导入导航,即使我在 Navigation.push< 中提供了参数,尝试在我的屏幕上访问 {route} 时显示路线未定义。

我的屏幕设置类似于以下内容:

const Tab = createBottomTabNavigator();
const WelcomeStack = createStackNavigator();
const HomeStack = createStackNavigator();
const SettingsStack = createStackNavigator();
const Stack = createStackNavigator();

const WelcomeStackScreen = () => (
<WelcomeStack.Navigator>
<WelcomeStack.Screen
name="Welcome"
component={WelcomeScreen}
options={{ headerShown: false }}
/>
<WelcomeStack.Screen
name="SignUp"
component={SignUpScreen}
options={{ headerShown: false }}
/>
</WelcomeStack.Navigator>
);

const HomeStackScreen = () => {
return (
<HomeStack.Navigator>
<HomeStack.Screen name="Home" component={HomeScreen} />
<HomeStack.Screen
name="Search"
component={SearchResultScreen}
options={({ navigation }) => ({
headerLeft: () => (
<Ionicons
name={"arrow-back"}
size={30}
color={colours.text}
style={{ paddingLeft: 15 }}
onPress={() => {
navigation.goBack();
}}
/>
),
})}
/>
<HomeStack.Screen
name="Select"
component={SelectScreen}
options={({ navigation, route }) => ({
title: route.params.name,
headerLeft: () => (
<Ionicons
name={"arrow-back"}
size={30}
color={colours.text}
style={{ paddingLeft: 15 }}
onPress={() => {
navigation.goBack();
}}
/>
),
})}
/>
<HomeStack.Screen name="Read" component={ReaderScreen} />
</HomeStack.Navigator>
);
};

function SettingsScreen() {
return (
<View
style={{ flex: 1, justifyContent: "center", alignItems: "center" }}
>
<Text>TBD</Text>
</View>
);
}

const SettingsStackScreen = () => (
<SettingsStack.Navigator>
<SettingsStack.Screen name="Settings" component={SettingsScreen} />
</SettingsStack.Navigator>
);

const TabNavigator = ({ route }) => (
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName;
switch (route.name) {
case "Home":
iconName = focused ? "home" : "home-outline";
break;
case "Settings":
iconName = focused ? "list" : "list-outline";
break;
default:
break;
}

return <Ionicons name={iconName} size={size} color={color} />;
},
})}
tabBarOptions={{
activeTintColor: "tomato",
inactiveTintColor: "gray",
}}
>
<Tab.Screen name="Home" component={HomeStackScreen} />
<Tab.Screen name="Settings" component={SettingsScreen}/>
</Tab.Navigator>
);

export default function App() {
return (
<NavigationContainer theme={navThemeOverride}>
<Stack.Navigator>
<Stack.Screen name="Welcome" component={WelcomeStackScreen} />
<Stack.Screen
name="TabNavigator"
component={TabNavigator}
options={{ headerShown: false }}
navigationOptions={{ gesturesEnabled: false }}
/>
</Stack.Navigator>
</NavigationContainer>
);
}

以下是欢迎屏幕导航的片段

navigation.push("TabNavigator", {
screen: "Home",
params: {
userId: userId,
},
});

最佳答案

你试图从你的父选项卡导航器导航到的主屏幕......也是一个 StackNavigator ......你想导航到 Select 屏幕我猜......所以您的导航需要一个额外的级别才能工作...

   navigation.navigate('TabNavigator', {
screen: 'Home', // <--- StackNavigator
params: {
screen: 'Select', // <-- nested inside HomeStack
params: {
title: 'Your custom title for Select screen here ...',
},
},
});

加号 +

Tab 导航器中的 route 有双重定义

const TabNavigator = ({ route }) => ( //<--- here
<Tab.Navigator
screenOptions={({ route }) => ({ // <- and here

改为

function TabNavigator() {
return <Tab.Navigator screenOptions={({ route }) => ({})>{/* ...Tabs... */}</Tab.Navigator>;
}

关于javascript - 嵌套在堆栈导航器中的 React Native 选项卡导航器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66237614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com