简体   繁体   English

如何使用 react-navigation v5 设置初始状态以将屏幕包含为历史记录?

[英]How can I set the initial state using react-navigation v5 to include screens as history?

Let's say I have the following route structure:假设我有以下路线结构:

Authenticated
    - Main
        - StackA
            -ViewA
        - StackB
            -ViewB
        - StackC
            - ViewC
            - ViewD

I want to set my initial state so that I'm looking at ViewD but I can swipe from the left to go back to ViewC.我想设置我的初始状态,以便我查看 ViewD,但我可以从左侧滑动以返回 ViewC。

The documentation here https://reactnavigation.org/docs/navigation-state/#partial-state-objects says that I should be able to provide an array of objects, each of which have a name property mapping to a route name, to the routes property of the initial state and it will sort the rest out for me.此处的文档https://reactnavigation.org/docs/navigation-state/#partial-state-objects说我应该能够提供一组对象,每个对象都有一个映射到路由名称的name属性,以初始状态的routes属性,它将为我整理其余部分。 However, when I do so, nothing changes and the usual initial state is rendered.但是,当我这样做时,没有任何变化,并且呈现通常的初始状态。

This is the initial state that I would expect to work, based on the documentation linked above:根据上面链接的文档,这是我希望工作的初始状态:

{
  routes: [
    { name: "Authenticated" },
    { name: "Main" },
    { name: "StackC" },
    { name: "ViewC" },
    { name: "ViewD" }
  ]
}

Edit编辑
I managed to get around the issue by using the following initial state:我通过使用以下初始状态设法解决了这个问题:

{
  routes: [
    {
      name: "Authenticated",
      params: {
        screen: "StackC",
        params: {
          screen: "ViewD",
          initial: false,
        }
      }
    }
  ]
}

The key part of this is the initial property.其中的关键部分是initial属性。 This causes the initial route of StackC to be rendered underneath ViewD , which is exactly what I wanted.这导致StackC的初始路由在StackCViewD ,这正是我想要的。

Here's a quick example I created on snack .这是我在零食上创建的一个快速示例。 Basically, you need to create an array of screens, and use the reset function.基本上,您需要创建一个屏幕数组,并使用重置功能。

    const initialState = {
    index: 4,
    routes: [
      {
        name: 'Stack',
        params: { screen: 'StackView1' },
      },
      {
        name: 'Stack',
        params: { screen: 'StackView2' },
      },
      {
        name: 'Stack1',
        params: { screen: 'Stack1View1' },
      },
      {
        name: 'Stack1',
        params: { screen: 'Stack1View2' },
      },
      {
        name: 'Stack1',
        params: { screen: 'Stack1View3' },
      },
    ],
  };
  return (
    <NavigationContainer initialState={initialState}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在本机反应中制作面包屑/显示屏幕路径(我正在使用反应导航 v5) - How can i make breadcrumbs/show screens path in react native (I am using react navigation v5) 如何在 react-navigation 5 中设置背景颜色 - How can I set the background color in react-navigation 5 反应导航获取屏幕中的当前导航状态 - React-navigation get current navigation state in screens react-navigation v5:抛出异常:createStackNavigator 不是函数 - react-navigation v5: Exception thrown: createStackNavigator is not a function 在 BottomTabNavigator react-navigation v5 内嵌套 StackNavigator - Nesting StackNavigator inside BottomTabNavigator react-navigation v5 @react-navigation/native 升级到 V5 SyntaxError - @react-navigation/native upgrade to V5 SyntaxError 如何在 react-navigation v4 中使用 react-native-screens? - How to use react-native-screens with react-navigation v4? 如何使用 react native 和 react navigation v5 设置背景图像? - How to set a background image with react native and react navigation v5? React Navigation V5 在特定屏幕中隐藏底部选项卡 - React Navigation V5 Hide Bottom Tab in Specific Screens 即使遵循 react-navigation 文档,react-navigation 也无法在屏幕之间导航 - react-navigation can't navigate between screens even after following react-navigation docs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM