【问题标题】:In react-navigation what's the difference between routeName and key?在 react-navigation 中,routeName 和 key 有什么区别?
【发布时间】:2017-02-22 19:51:54
【问题描述】:

有点令人困惑的是路线名称和密钥之间的区别,以及为什么要使用一个与另一个。以及如何处理重复的路线名称。

This documentation says 您使用routeName 导航到屏幕,并且key 是“用于对路线进行排序的唯一标识符”。这是什么意思?

似乎路由名称不必是唯一的,如我的示例所示,因为外部选项卡和内部堆栈具有相同的路由名称。当您使用导航功能时 - 您传递了路线名称,对吗?如果是这样,它如何区分嵌套导航器中的重复路由名称以及何时使用该键?

        export TabsNavigator = TabNavigator({
          Home: {
            screen:StackNavigator({
              Home: { screen: HomeScreen },
            }),
          },
          Profile: {
            screen: StackNavigator({
              Profile: { ProfileScreen },
            }),
          },
        });

The documentation 有一个设置密钥的示例,但我无法理解它试图做什么的上下文,或者你为什么要在实际用例中这样做。

import { NavigationActions } from 'react-navigation'

const setParamsAction = NavigationActions.setParams({
  params: {}, // these are the new params that will be merged into the existing route params
  // The key of the route that should get the new params
  key: 'screen-123',
})
this.props.navigation.dispatch(setParamsAction)

【问题讨论】:

    标签: react-native react-navigation ex-navigation


    【解决方案1】:

    您使用导航器中指定的屏幕名称(例如StackNavigator)打开/显示屏幕。每个屏幕都有一个唯一的标识符,这是关键。例如。如果您打开两个相同类型的屏幕,它们将具有相同的路由名称,但不同的键。

    使用this.props.navigation.dispatch(NavigationActions.setParams(params: {val: 'val'}, key: 'home-1'));,您可以使用'home-1' 键更新屏幕的导航状态。例如。如果您在主屏幕顶部有一个 StackNavigator 和一个设置屏幕,您可以从设置屏幕更新主屏幕的导航状态 (this.props.navigation.state.params)。

    【讨论】:

    • 如果我不使用 NavigationActions,我应该在哪里指定一个键?是否在 StackNavigator 路由定义中?
    • 唯一键在哪里配置?是每个屏幕还是每个导航器?文档在按键上非常糟糕......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2013-09-22
    • 2012-11-20
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    相关资源
    最近更新 更多