一旦您导航到嵌套堆栈导航的初始路由组件以外的另一个组件,navigation.navigate('Nested Navigator 2');
不管用
初始导航堆栈将在初始路线上显示此内容
[
{ name: ScreenA },
{ name: ScreenC },
{ name: ScreenE }
]
但是每当您从屏幕 C 导航到屏幕 D 时,屏幕 D 的路由都不会插入到堆栈的末尾,因为它是嵌套导航两个
[
{ name: ScreenA },
{
name: NestedNavigator,
[
{ name: screenC },
{ name: screenD }
]
},
{ name: ScreenE }
]
所以你需要使用,
navigation.navigate('嵌套导航器 2', { screen: 'screen D' });
这是因为当您导航到初始路线以外的屏幕时,堆栈会发生变化,
您将需要在嵌套组件上重置路由,即每当您导航到屏幕 D 时,使用 commonActions 重置路由堆栈,设置屏幕,这将更新堆栈,并从堆栈中删除屏幕 D。
import { CommonActions } from '@react-navigation/native';
componentDidMount() {
this.props.navigation.addListener('blur', () => {
this.props.navigation.dispatch(
CommonActions.reset({
index: 1,
routes: [
{ name: 'ScreenC' },
],
})
);
});
}
如果您只想删除某个路线而不是重置整个导航
this.props.navigation.dispatch(state => {
// Remove the route from the stack
const routes = state.routes.filter((r => r.name !== 'ScreenC' ));
return CommonActions.reset({
...state,
routes,
index: routes.length - 1,
});
});