[英]React Native: How to remove the current stack navigator from the navigation-stack
I am struggling with a situation.我正在为一种情况而苦苦挣扎。 So I have three stack navigators.
所以我有三个堆栈导航器。
navigator1 => screen1导航器 1 => 屏幕 1
navigator2 => screen2导航器 2 => 屏幕 2
navigator3 => screen3, screen4, screen5导航器 3 => 屏幕 3、屏幕 4、屏幕 5
I navigate from screen1
to screen2
.我从
screen1
导航到screen2
。 And then from screen2
to screen3
then screen4
then screen5
and then navigate to screen2
again.然后从
screen2
到screen3
3,然后是screen4
4,然后是screen5
,然后再次导航到screen2
。 But now when I am on screen2
, onBack press I don't want to go on screen5
, instead want to go directly on screen1
.但是现在当我在
screen2
上时,onBack 按我不想在 screen5 上screen5
,而是想直接在 screen1 上screen1
。
When I tried to pop screen5
, screen4
and screen3
from stack just before navigating to screen2
from screen5
using the follwing code.当我试图在使用以下代码从
screen5
导航到screen2
之前从堆栈中弹出screen5
、 screen4
和screen3
时。 It didn't work, still screen3
remains on stack.它没有用,仍然
screen3
仍然在堆栈上。
import { StackActions } from '@react-navigation/native';
navigation.dispatch(
StackActions.popToTop()
);
OR要么
import { StackActions } from '@react-navigation/native';
navigation.dispatch(
StackActions.pop(3)
);
How to remove the navigator3
from the navigation stack?如何从导航堆栈中删除
navigator3
? So that I would not go on screen5
from screen2
again onBackPress这样我就不会再次在 screen2 的
screen2
上screen5
onBackPress
I am using react-navigation version 5.x.我正在使用 react-navigation 版本 5.x。
Check out this example it works as you want查看此示例,它可以按需工作
import * as React from 'react';
import { View, Button, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
const Stack = createStackNavigator();
const A = ({ navigation }) => {
return (
<View>
<Text>A</Text>
<Button title={'Next'} onPress={() => navigation.navigate('Second')} />
</View>
);
};
const B = ({ navigation }) => {
return (
<View>
<Text>B</Text>
<Button
title={'Next'}
onPress={() => navigation.navigate('Third', { screen: 'C' })}
/>
</View>
);
};
const C = ({ navigation }) => {
return (
<View>
<Text>C</Text>
<Button
title={'Next'}
onPress={() => navigation.navigate('Third', { screen: 'D' })}
/>
</View>
);
};
const D = ({ navigation }) => {
return (
<View>
<Text>D</Text>
<Button
title={'Next'}
onPress={() => navigation.navigate('Third', { screen: 'E' })}
/>
</View>
);
};
const E = ({ navigation }) => {
return (
<View>
<Text>E</Text>
<Button title={'Next'} onPress={() => navigation.navigate('Second')} />
</View>
);
};
const FirstStackScreen = () => {
return (
<Stack.Navigator>
<Stack.Screen name="A" component={A} />
</Stack.Navigator>
);
};
const SecondStackScreen = () => {
return (
<Stack.Navigator>
<Stack.Screen name="B" component={B} />
</Stack.Navigator>
);
};
const ThirdStackScreen = () => {
return (
<Stack.Navigator>
<Stack.Screen name="C" component={C} />
<Stack.Screen name="D" component={D} />
<Stack.Screen name="E" component={E} />
</Stack.Navigator>
);
};
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="First" component={FirstStackScreen} />
<Stack.Screen name="Second" component={SecondStackScreen} />
<Stack.Screen name="Third" component={ThirdStackScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
Why don't you navigate to directly Screen1
when user click back
button?当用户单击
back
按钮时,为什么不直接导航到Screen1
?
const onBack = () => {
navigation.navigate('Screen1');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.