简体   繁体   English

使用react-navigation在react native中的不同导航器之间跳转

[英]Jumping between different navigators in react native using react-navigation

I have 3 Navigators. 我有3个导航器。 The MainNavigator is the root of the other two (Auth and Activities). MainNavigator是其他两个(Auth和Activity)的根。 Based on whether the user has successfully logged in, I need to route the user from the Auth Stack to the Activities stack. 根据用户是否已成功登录,我需要将用户从“身份验证”堆栈路由到“活动”堆栈。 How can I do that? 我怎样才能做到这一点? I can't seem to figure this out just yet (new to react-native, coming from Angular). 我似乎还不能弄清楚(来自Angular的React-native新手)。

Here's the line of code that I use to navigate to the Activities stack: 这是我用来导航到“活动”堆栈的代码行:

        this.props.navigation.navigate('Activities');

Auth: 验证:

export const AuthNavigator = StackNavigator({
    Login: {
        screen: Login,
    },
    SignUp: {
        screen: SignUp
    },
    Confirm: {
        screen: ConfirmSignUp
    }
}, {
    initialRouteName: 'Login',
    headerMode: 'none',
    animationEnabled: 'true'
});

Activities: 活动内容:

export const ActivitiesNavigator = TabNavigator({
    ActivityList: {
        screen: ActivityList
    },
    MyActivityList: {
        screen: MyActivityList
    },
    CreateActivity: {
        screen: CreateActivity
    }
}, {
    initialRouteName: 'ActivityList'
});

Main Navigator: 主导航器:

export const MainNavigator = StackNavigator({
    Auth: { screen:  AuthNavigator},
    Activities: { screen: ActivitiesNavigator },
}, {
    initialRouteName: 'Auth',
    headerMode: 'none',
    animationEnabled: 'true'
});

This actually works. 这实际上有效。 Lack of sleep made me put the 'navigate' command in a different method. 睡眠不足使我将“ navigate”命令置于另一种方法中。 So in my App.js file I just need to return the MainNavigator and I'm good to go: 所以在我的App.js文件中,我只需要返回MainNavigator就可以了:

render() {

    return (
        <MainNavigator/>
    );
}

YOU DON'T NEED 3 Navigators. 您不需要3个导航器。 this is the way to do it with React Navigation 这是使用React Navigation做到这一点的方法

export const AuthNavigator = StackNavigator({
        Login: {
            screen: Login,
        },
        SignUp: {
            screen: SignUp
        },
        Confirm: {
            screen: ConfirmSignUp
        },
        Main: {
            screen: ActivitiesNavigator
       }
    }, {
        initialRouteName: 'Login',
        headerMode: 'none',
        animationEnabled: 'true'
    });

Visit StackActions reset : The reset action wipes the whole navigation state and replaces it with the result of several actions. 访问StackActions reset:reset操作将擦除整个导航状态,并将其替换为若干操作的结果。

In Latest react-navigation version: 3.3.0, I have used: at Signup or Login so that to start Home/Main Flow 在最新的反应导航版本:3.3.0中,我已使用:在注册或登录时启动主流程/主流程

import { StackActions, NavigationActions } from 'react-navigation'; const resetAction = StackActions.reset({ index: 0, key: null, // <-this is imp. actions: [ NavigationActions.navigate({ routeName: 'Main' }) ], }); this.props.navigation.dispatch(resetAction);

Remember : Adding key: null means master route will be null and not on any route. 切记 :添加密钥:null表示主路由将为null,而不是任何路由。 So we are free to navigate to the route in any Nested Stacks 因此,我们可以自由导航到任何嵌套堆栈中的路线

key - string or null - optional - If set, the navigator with the given key will reset. key-字符串或null-可选-如果设置,具有给定键的导航器将重置。 If null, the root navigator will reset. 如果为null,则根导航器将重置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM