首页 > 解决方案 > React Native:注销时重定向到另一个堆栈导航

问题描述

我使用 asyncStorage 在我的应用程序中更改登录/退出状态,当我单击 logOut 时,我将状态更改为 false,然后我必须将用户重定向到另一个堆栈导航(loginStackNavigation)

<TouchableOpacity
style={[styles...]}
onPress={() => onSignOut().then(this.navigateToScreen('LoggingStackNavigation'))}
>
    ...
</TouchableOpacity>

附言。onSignOut() 函数更改存储在 asyncstorage 中的值

这是 navigateToScreen() 函数:

  navigateToScreen = (route) => (
  () => {
    const navigateAction = NavigationActions.navigate({
      routeName: route
    });
    this.props.navigation.dispatch(navigateAction);
    this.props.navigation.closeDrawer();
});

我该怎么做才能将用户重定向到LoggingStackNavigation

标签: react-nativereact-navigation

解决方案


我认为您应该使用 Switch Navigator 来包装您的 2 个导航器:

import { createSwitchNavigator } from 'react-navigation';

 const routesConfig = {
     NotAuthenticated :{screen: loggedOutStack},
     Authenticated : { screen: loggedInStack}
  }

 const SwitchNavigatorConfig= {
     initialRouteName: "NotAuthenticated"
 }

export const createSwitchNavigator(RoutesConfig, SwitchNavigatorConfigs)

然后你可以navigation.navigate("NotAuthenticated")在需要的时候


推荐阅读