【发布时间】:2023-03-30 00:59:01
【问题描述】:
我在我的 react-native 项目中使用 React Navigation v5。
我有一个嵌套的堆栈导航器。
父堆栈导航器MyParentFlow 有一个屏幕组件,该组件承载另一个堆栈导航器:
const MyParentFlow = ({route, navigation}) => {
const MyStack = createStackNavigator();
return (
<MyStack.Navigator
initialRouteName={...}
...>
{/*HERE is the nested children stack navigator*/}
<MyStack.Screen
name={FLOWS.MyChildrenFlow}
component={MyChildrenFlow}
/>
<MyStack.Screen .../>
...
</MyStack.Navigator>
)
}
我的嵌套堆栈导航器MyChildrenFlow:
const MyChildrenFlow = ({route, navigation}) => {
const MyStack = createStackNavigator();
return (
<MyStack.Navigator
initialRouteName={...}
...>
{/*HERE is a child screen in the nested navigator*/}
<MyStack.Screen
name="MyChildScreen"
component={MyChildScreen}
/>
<MyStack.Screen .../>
...
</MyStack.Navigator>
)
}
在嵌套堆栈导航器托管的子屏幕中:
const MyChildScreen =({navigation})=> {
/* I need to set options for the header of the parent's navigation */
navigation.setOptions({
headerRight: () => (
...
/>
),
});
}
在嵌套堆栈导航器托管的子屏幕中,我需要通过navigation 为父导航器设置标题。我上面的代码不起作用,因为 navigation 对象是嵌套堆栈导航器的,而不是父级的。
我的问题是如何在嵌套的子屏幕中访问父导航器并为其设置导航选项?
【问题讨论】:
标签: react-native react-navigation react-navigation-v5