【问题标题】:native-stack of react-navigation 6 does not find a route objectreact-navigation 6的native-stack找不到路由对象
【发布时间】:2022-01-26 18:51:37
【问题描述】:

在react-navigation 6的“react-navigation/stack”下,它很好地处理了useRoute,useNavigation..etc等所有功能

但是,我将堆栈导航从 react-navigation/stack 更改为 react-navigation/native-stack。

native-stack 在“GroupHeaders 组件”中没有找到路由对象

在我的代码下面。

  1. 根导航
import { createNativeStackNavigator, NativeStackHeaderProps } from "@react-navigation/native-stack";

const RootNavigation = createNativeStackNavigator();
    <NavigationContainer>
        <RootNavigation.Navigator>
          <RootNavigation.Screen
            name="Main-root"
            component={TabNavigation}
            options={{ headerShown: false }}
          />
        
          <RootNavigation.Screen
            name="PostDetail-root"
            component={PostDetailContainer}
            options={{ header: () => <GroupHeaders /> }}. // ***** HERE
          />
         // ...other screen...

  1. 标签导航
      <Tab.Navigator
        initialRouteName="home-tab"
        //...
        >
        <Tab.Screen
          name="home-tab"
          component={HomeNavigation}
          options={{
            headerShown: false,
            tabBarLabel: "Home",
            //...
          }}
        />
  1. GroupHeaders 组件
const GroupHeaders = ({}) => {
  const route = useRoute(); //**** HERE, RN occurs error message of "~not find a route object~~" 
  // ... other code

【问题讨论】:

    标签: react-native react-navigation react-native-reanimated-v2


    【解决方案1】:

    你可以使用选项参数

    reference document.

    <RootNavigation.Screen
        name="PostDetail-root"
        component={PostDetailContainer}
        options={({ route }) => ({
        tabBarLabel: (props) => <GroupHeaders route={route} {...props}  />,
      })}/> // ***** HERE
    />
    
    • 使用
      function GroupHeaders({route}) {
        console.log("route1",route);
        return (
            <Text>{route.name}</Text>
        )
      }
    

    【讨论】:

    • 感谢您的回答。但是,您的回答并没有解决我的问题。 RN 显示错误消息,例如“找不到路由对象”。 @hong 开发者
    • @rudenick 我修改了我的答案。我给你做了一个测试。 snack.expo.dev/nS7BuW3E18
    猜你喜欢
    • 2022-10-23
    • 1970-01-01
    • 2022-10-25
    • 2021-11-02
    • 1970-01-01
    • 2022-01-04
    • 2021-01-24
    • 2017-11-28
    • 1970-01-01
    相关资源
    最近更新 更多