【问题标题】:How to define nested navigation prop of react-native v5?如何定义 react-native v5 的嵌套导航道具?
【发布时间】:2021-06-27 01:45:24
【问题描述】:

我正在尝试定义 react-navigation 的类型(导航和路由道具),以便更容易自动完成 VS Code。

App(Stack) > Main(BottomTab)> [Home(Stack){Post}, Profile(Stack){UserProfile}] 在这种嵌套导航状态下,我想从 Post(Home) 导航到 UserProfile(Profile)。

这是我的 Stack 和 BottomTab 参数列表。

/* App Navigation Types */
export type AppStackParamList = {
  Landing: undefined;
  Main: undefined;
};

/* Main Navigation Types */
export type MainTabParamList = {
  Home: undefined;
  Crypto: undefined;
  Posting: undefined;
  Chat: undefined;
  Profile: undefined;
};

/* Home Navigation Type */
export type HomeStackParamList = {
  Post: { id: string } | undefined;
  CategoryFilter: undefined;
  Feed: undefined;
  Search: undefined;
  SetLocation: undefined;
};

/* Profile Navigation Type */
export type ProfileStackParamList = {
  EditProfile: undefined;
  MyPage: undefined;
  UserProfile: { id: string } | undefined;
};

这些是每个屏幕的导航道具类型

export type PostNavProp = CompositeNavigationProp<
  StackNavigationProp<HomeStackParamList, 'Post'>,
  BottomTabNavigationProp<MainTabParamList>
>;

export type UserProfileNavProp = CompositeNavigationProp<
  StackNavigationProp<ProfileStackParamList, 'UserProfile'>,
  BottomTabNavigationProp<MainTabParamList>
>;

使用这种类型定义,navigation.navigate('Profile') 可以工作。但配置文件的初始屏幕路径不是“用户配置文件”。 当我尝试使用导航()函数的第二个参数(如navigation.navigate('Profile', {screen:'UserProfile'}))访问 UserProfile 时,它​​显示错误..

only 1st parameter works error message

我可以就这个问题获得一些建议吗?我需要立即帮助..拜托..

【问题讨论】:

    标签: typescript react-navigation-v5


    【解决方案1】:

    您的设置似乎正确。 使用 react-navigation 的嵌套导航器似乎存在已知类型错误:

    https://github.com/react-navigation/react-navigation/issues/6931

    【讨论】:

      猜你喜欢
      • 2017-10-20
      • 2021-10-13
      • 1970-01-01
      • 2020-12-06
      • 2021-12-23
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 2017-10-12
      相关资源
      最近更新 更多