反应导航嵌套堆栈routeName

时间:2018-10-10 06:49:02

标签: javascript reactjs react-native react-navigation

我想获取有效路由的routeName并在标题中显示,所以我使用:

navigationOptions: ({ navigation }) => ({
  header: (
    <Header
      title={navigation.state.routes[navigation.state.index].routeName}
    />
  )
})

请注意,<Header>是我创建的自定义组件。

问题是我无法访问嵌套堆栈中的routeName

TypeError: TypeError: TypeError: TypeError: TypeError: undefined is not an object (evaluating 'navigation.state.routes[navigation.state.index]')

有什么建议吗?

  

提前谢谢!


更新

我的Router.js

const RootStack = createStackNavigator(
  {
    _Landing: {
      screen: TabNavigator(
        {
          a: Splash,
          b: Splash,
          c: createStackNavigator(
            {
              d: Services,
              e: Splash
            },
            {
              initialRouteName: "d",
              navigationOptions: ({ navigation }) => ({     // HERE is the problem
                header: (
                  <Header
                    title={
                      navigation.state.routes[navigation.state.index].routeName
                    }
                  />
                )
              }),
              transitionConfig: getSlideFromRightTransition
            }
          ),
          f: Splash,
          g: Splash
        },
        {
          tabBarPosition: "bottom",
          initialRouteName: "c",
          animationEnabled: true,
          swipeEnabled: true,
          lazy: false
        }
      )
    }
  },
  {
    initialRouteName: "_Landing",
    navigationOptions: ({ navigation }) => ({
      header: (
        <Header
          title={navigation.state.routes[navigation.state.index].routeName}
        />
      )
    }),
    transitionConfig: getSlideFromRightTransition
  }
);

请注意,问题出在// Here is the problem部分。最后一个navigationOptions正在工作!

0 个答案:

没有答案