我想获取有效路由的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
正在工作!