抽屉导航
导航中的常见模式是使用左侧(有时是右侧)的抽屉在屏幕之间导航。
¥Common pattern in navigation is to use drawer from left (sometimes right) side for navigating between screens.
在继续之前,首先安装并配置 @react-navigation/drawer
及其依赖,然后安装 安装说明。
¥Before continuing, first install and configure @react-navigation/drawer
and its dependencies following the installation instructions.
基于抽屉的导航的最小示例
¥Minimal example of drawer-based navigation
要使用此抽屉式导航器,请从 @react-navigation/drawer
导入它:(向右滑动打开)
¥To use this drawer navigator, import it from @react-navigation/drawer
:
(swipe right to open)
- Static
- Dynamic
import * as React from 'react';
import { View } from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import {
createStaticNavigation,
useNavigation,
} from '@react-navigation/native';
import { Button } from '@react-navigation/elements';
function HomeScreen() {
const navigation = useNavigation();
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => navigation.navigate('Notifications')}>
Go to notifications
</Button>
</View>
);
}
function NotificationsScreen() {
const navigation = useNavigation();
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => navigation.goBack()}>Go back home</Button>
</View>
);
}
const Drawer = createDrawerNavigator({
screens: {
Home: HomeScreen,
Notifications: NotificationsScreen,
},
});
const Navigation = createStaticNavigation(Drawer);
export default function App() {
return <Navigation />;
}
import * as React from 'react';
import { View } from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { Button } from '@react-navigation/elements';
function HomeScreen() {
const navigation = useNavigation();
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => navigation.navigate('Notifications')}>
Go to notifications
</Button>
</View>
);
}
function NotificationsScreen() {
const navigation = useNavigation();
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => navigation.goBack()}>Go back home</Button>
</View>
);
}
const Drawer = createDrawerNavigator();
export default function App() {
return (
<NavigationContainer>
<Drawer.Navigator initialRouteName="Home">
<Drawer.Screen name="Home" component={HomeScreen} />
<Drawer.Screen name="Notifications" component={NotificationsScreen} />
</Drawer.Navigator>
</NavigationContainer>
);
}
打开和关闭抽屉
¥Opening and closing drawer
要打开和关闭抽屉,请使用以下助手:
¥To open and close drawer, use the following helpers:
- Static
- Dynamic
<Button onPress={() => navigation.openDrawer()}>Open drawer</Button>
/* content */
<DrawerItem
label="Close drawer"
onPress={() => props.navigation.closeDrawer()}
/>
<Button onPress={() => navigation.openDrawer()}>Open drawer</Button>
/* content */
<DrawerItem
label="Close drawer"
onPress={() => props.navigation.closeDrawer()}
/>
如果你想切换抽屉,请调用以下命令:
¥If you would like to toggle the drawer you call the following:
- Static
- Dynamic
<Button onPress={() => navigation.toggleDrawer()}>Toggle drawer</Button>
<Button onPress={() => navigation.toggleDrawer()}>Toggle drawer</Button>
这些函数中的每一个都在幕后简单地调度操作:
¥Each of these functions, behind the scenes, are simply dispatching actions:
- Static
- Dynamic
<Button onPress={() => navigation.dispatch(DrawerActions.openDrawer())}>
Open drawer
</Button>
/* content */
<DrawerItem
label="Close drawer"
onPress={() => props.navigation.dispatch(DrawerActions.closeDrawer())}
/>
<DrawerItem
label="Toggle drawer"
onPress={() => props.navigation.dispatch(DrawerActions.toggleDrawer())}
/>
<Button onPress={() => navigation.dispatch(DrawerActions.openDrawer())}>
Open drawer
</Button>
/* content */
<DrawerItem
label="Close drawer"
onPress={() => props.navigation.dispatch(DrawerActions.closeDrawer())}
/>
<DrawerItem
label="Toggle drawer"
onPress={() => props.navigation.dispatch(DrawerActions.toggleDrawer())}
/>
如果你想确定抽屉是打开还是关闭,你可以执行以下操作:
¥If you would like to determine if drawer is open or closed, you can do the following:
import { useDrawerStatus } from '@react-navigation/drawer';
// ...
const isDrawerOpen = useDrawerStatus() === 'open';