gpt4 book ai didi

react-native - react native : How to remove the current stack navigator from the navigation-stack

转载 作者:行者123 更新时间:2023-12-04 01:15:34 25 4
gpt4 key购买 nike

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

  • 导航器 1 => 屏幕 1
  • 导航器 2 => 屏幕 2
  • 导航器 3 => 屏幕 3、屏幕 4、屏幕 5

  • 我从 screen1 导航至 screen2 .然后从 screen2screen3然后 screen4然后 screen5然后导航到 screen2再次。但是现在当我在 screen2 , onBack 按我不想继续 screen5 , 而是想直接上 screen1 .
    当我试图弹出 screen5 , screen4screen3在导航到 screen2 之前从堆栈来自 screen5使用以下代码。没用,还是 screen3留在堆栈中。
      import { StackActions } from '@react-navigation/native';

    navigation.dispatch(
    StackActions.popToTop()
    );
      import { StackActions } from '@react-navigation/native';

    navigation.dispatch(
    StackActions.pop(3)
    );
    如何删除 navigator3从导航堆栈?以免我继续下去 screen5来自 screen2再次在BackPress
    我正在使用 react-navigation 版本 5.x。

    最佳答案

    看看这个例子,它可以如你所愿

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

    关于react-native - react native : How to remove the current stack navigator from the navigation-stack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63474447/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com