React Native:如何从导航堆栈中删除当前的堆栈导航器

时间:2020-08-18 18:20:18

标签: react-native react-redux react-native-android react-navigation react-navigation-v5

我正在为一种情况而苦苦挣扎。所以我有三个堆栈导航器。

  • navigator1 =>屏幕1

  • navigator2 =>屏幕2

  • navigator3 =>屏幕3,屏幕4,屏幕5

我从screen1导航到screen2。然后从screen2screen3,然后是screen4然后是screen5,然后再次导航到screen2。但是现在当我在screen2上时,按onBack键我不想继续在screen5上,而是想直接在screen1上进行。

当我尝试使用下面的代码从screen5导航到screen4之前,从堆栈中弹出screen3screen2screen5时。它没有用,screen3仍然保留在堆栈中。

  import { StackActions } from '@react-navigation/native';

  navigation.dispatch(
       StackActions.popToTop()
  );

OR

  import { StackActions } from '@react-navigation/native';

  navigation.dispatch(
       StackActions.pop(3)
  );

如何从导航堆栈中删除navigator3?这样我就不会再在BackBackPress上从screen5继续进行screen2

我正在使用反应导航版本5.x。

2 个答案:

答案 0 :(得分:2)

查看此示例,即可根据需要运行

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>
  );
}

答案 1 :(得分:2)

为什么当用户单击Screen1按钮时您不能直接导航到back

const onBack = () => {
   navigation.navigate('Screen1');
}