简体   繁体   English

在反应导航中导航到其他堆栈

[英]Navigate to a different stack in react navigation

I have a Switch Navigator and Bottom Tab Navigator. 我有一个Switch Navigator和Bottom Tab Navigator。 The Swich Navigator has the login screen and the Bottom Tab Navigator has the home screens and logout screen. Swich Navigator具有登录屏幕,Bottom Tab Navigator具有主屏幕和注销屏幕。

Switch Navigator: 切换导航器:

const RootStack = createSwitchNavigator(
  {
    AuthLoadingScreen: AuthLoadingScreen,
    Auth: AuthStack,
    AADB2CLogin: AADB2CLogin,
    Home: mainBottomStack
  },
  {
    initialRouteName: "AuthLoadingScreen",

    transitionConfig
  }
);

Bottom Tab Navigator: 底部标签浏览器:

    const mainBottomStack = createBottomTabNavigator(
  {
    Home: mainStack,
    MedicalRecord: MedicalRecordStack,
    //MedicalRecord: PatientDetails,
    Visit: VisitStack,
    Alerts: AlertStack,
    Profile: PatientDetails,
    //Settings: Logout
    Logout: {
      screen: () => null,
      navigationOptions: {
        tabBarOnPress: () => {
          Alert.alert(
            "Logout",
            "Are you sure you want to logout?",
            [
              {
                text: "No",
                style: "cancel"
              },
              {
                text: "Yes",
                onPress: () => {
                  console.log("logout");
                  //I want to navigate to switch navigator's Auth screen here...
                }
              }
            ],
            { cancelable: false }
          );
        }
      }
    }
  },
  {
    transitionConfig,
    initialRouteName: "Home",
    barStyle: { backgroundColor: "#694fad" }
  }
);

On logout, in bottom tab navigator, I want to navigate to Switch navigator (to Auth screen). 在注销时,在底部标签导航器中,我想导航至“切换导航器”(至“身份验证”屏幕)。 How can navigate between different stacks in react navigation? 如何在反应导航中的不同堆栈之间导航?

Can you change to the following? 您可以更改以下内容吗?

Add your TabNavigation 'mainBottomStack' in to the SwitchNavigation 将您的TabNavigation '添加到SwitchNavigation

const RootStack = createSwitchNavigator(
  {
    AuthLoadingScreen: AuthLoadingScreen,
    Auth: AuthStack,
    AADB2CLogin: AADB2CLogin,
    Home: mainBottomStack,
    TabNavigation: mainBottomStack
  },
  {
    initialRouteName: "AuthLoadingScreen",

    transitionConfig
  }
);

Then navigate to 'Auth' screen like the following, 然后导航到“验证”屏幕,如下所示,

this.props.navigation.navigate("Auth");

You can make it work by doing the following in createBottomTabNavigator : 您可以通过在createBottomTabNavigator执行以下操作使其工作:

Logout: {
  screen: () => null,
  navigationOptions: ({ navigation }) => ({
    tabBarOnPress: () => {
      Alert.alert(
        "Logout",
        "Are you sure you want to logout?",
        [
          {
            text: "No",
            style: "cancel"
          },
          {
            text: "Yes",
            onPress: () => {
              //console.log("logout");
              AsyncStorage.setItem("token", null);
              navigation.navigate("Auth");
            }
          }
        ],
        { cancelable: false }
      );
    }
  })
}

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

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