React Navigation-如何将抽屉导航器嵌套在stackNavigator中

时间:2019-04-11 01:04:22

标签: reactjs react-native react-navigation drawer

我正在使用反应导航,我想从我的个人资料屏幕打开一个抽屉,可以选择进入 EditProfile 屏幕和设置 >屏幕。但是,当我点击 MyProfile的标题按钮时,我不知道如何打开抽屉。

App.js:

const MyProfileStack = createStackNavigator({
  MyProfile: {
    screen: profile,
    navigationOptions: ({navigation}) => {
      return {
        title: "My Profile",
        headerRight: (
          <Icon type="evilicon" name="navicon" size={40}
          onPress={() => navigation.dispatch(DrawerActions.openDrawer())}/>
        )
      };
    }
  }
})

const DrawerStack = createDrawerNavigator({
  Edit: { screen: EditProfileStack }
  Settings: { screen: SettingsStack }
})

const EditProfileStack = createStackNavigator({ 
  EditProfile: {
    screen: editProfile,
    navigationOptions: ({navigation}) => {
      return {
        title: "Edit Profile",
        headerLeft: (
          <Icon type="evilicon" name="chevron-left" size={50}
          onPress={() => navigation.navigate("MyProfile")}/>
        )
      };
    }
  }
});

const TabStack = createBottomTabNavigator({
  Feed: { screen: FeedStack },
  Profile: { screen: MyProfileStack },
});

const MainStack = createSwitchNavigator(
  {
    Home: TabStack,
    Drawer: DrawerStack
  },
  {
    initialRouteName: 'Home'
  }
);

const AppContainer = createAppContainer(MainStack);

1 个答案:

答案 0 :(得分:0)

解决方案

您需要将MyProfileStack放在DrawerStack中,如下所示。

const DrawerStack = createDrawerNavigator({
  MyProfile: { screen: MyProfileStack }
  Edit: { screen: EditProfileStack }
  Settings: { screen: SettingsStack }
})


const TabStack = createBottomTabNavigator({
  Feed: { screen: FeedStack },
  Profile: { screen: DrawerStack },
});

const AppContainer = createAppContainer(MainStack);

您可以使用各种组合。

为什么?

当您切换到另一个屏幕时,SwitchNavigator会退出其他屏幕。因此,您无法从已经辞职的屏幕中调用抽屉。

p.s:如果要在更改屏幕时刷新屏幕,则可以使用navigation events。使用onWillFocus