React Native:注销后重定向到另一个堆栈导航

时间:2019-06-25 15:03:30

标签: react-native react-navigation

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

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

ps。 onSignOut()函数更改存储在异步存储中的值

这是navigationToScreen()函数:

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

如何将用户重定向到LoggingStackNavigation

2 个答案:

答案 0 :(得分:1)

我认为您应该使用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")

答案 1 :(得分:0)

您的函数需要调用navigation.navigate

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