React native nested navigation - 单击父抽屉路由时返回到堆栈导航器的初始路由
React native nested navigation - go back to initial route of stack navigator when the parent drawer route is clicked
我在我的 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)
场景:
- 我在抽屉中单击 "Home" link 并导航至 MainScreen。
- 我在主屏幕上单击一个按钮并导航到 FriendsListScreen。
- 在 FriendsListScreen 中,我再次打开抽屉并单击 "Home" link。抽屉关闭,没有导航发生。
模型:
当我从抽屉中单击 "Home" link 时,有没有办法导航回主屏幕?
我通过如下创建自定义抽屉内容得到了解决方法:
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});
我在我的 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)
场景:
- 我在抽屉中单击 "Home" link 并导航至 MainScreen。
- 我在主屏幕上单击一个按钮并导航到 FriendsListScreen。
- 在 FriendsListScreen 中,我再次打开抽屉并单击 "Home" link。抽屉关闭,没有导航发生。
模型:
当我从抽屉中单击 "Home" link 时,有没有办法导航回主屏幕?
我通过如下创建自定义抽屉内容得到了解决方法:
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});