0

我在我的 react native 项目中使用以下导航设置。在这里,我使用抽屉导航和堆栈导航。

import MainScreen from '../components/MainScreen';
import SettingsScreen from '../components/SettingsScreen';
import FriendsListScreen from '../components/FriendsListScreen';
import FriendProfileScreen from '../components/FriendProfileScreen';

// stack navigator - 1
const MainScreenStack = createStackNavigator(
    {
        MainScreen: MainScreen,
        FriendsListScreen: FriendsListScreen,
        FriendProfileScreen: FriendProfileScreen,
    },
    {
        initialRouteName: 'MainScreen',
        navigationOptions: {
            drawerLabel: "Home"
        },
    }
);

// stack navigator - 2
const SettingsScreenStack = createStackNavigator(
    {
        SettingsScreen: SettingsScreen
    },
    {
        navigationOptions: {
            drawerLabel: "Settings"
        },
    }
);

// drawer navigation (stack navigator 1 + 2)
const AppNavigator = DrawerNavigator({
  MainScreen: MainScreenStack,
  SettingsScreen: SettingsScreenStack
});

const AppContainer = createAppContainer(AppNavigator)

设想:

  • 从抽屉中,我单击“主页”链接并导航到 MainScreen。
  • 从 MainScreen,我单击一个按钮并导航到 FriendsListScreen。
  • 在 FriendsListScreen 中,我再次打开抽屉并单击“主页”链接。抽屉关闭,没有导航发生。

小样:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

当我从抽屉中单击“主页”链接时,有没有办法导航回主屏幕?

4

1 回答 1

0

我通过创建自定义抽屉内容来解决此问题,如下所示:

import {DrawerItems, SafeAreaView, createStackNavigator, createDrawerNavigator, createAppContainer} from "react-navigation";

const DrawerContent = (props) => (
  <ScrollView>
    <SafeAreaView forceInset={{top: 'always', horizontal: 'never'}}>
      <DrawerItems {...props} onItemPress={ ({route, focused}) => {
        if (route.key === "MainScreen") {
          if (route.routes.length === 1) {
            props.navigation.closeDrawer();
          } else {
            props.navigation.navigate(route.routes[0].routeName);
          }
        } else {
          props.onItemPress({route, focused});
        }
      }} />
    </SafeAreaView>
  </ScrollView>
);

const AppNavigator = DrawerNavigator({
  MainScreen: MainScreenStack,
  SettingsScreen: SettingsScreenStack
}, {contentComponent: DrawerContent});
于 2019-02-07T17:13:17.553 回答