Drawer navigation
Common pattern in navigation is to use drawer from left (sometimes right) side for navigating between screens.
Before continuing, first install and configure @react-navigation/drawer
and its dependencies following the installation instructions.
Minimal example of drawer-based navigation
To use this drawer navigator, import it from @react-navigation/drawer
:
(swipe right to open)
- Static
- Dynamic
import * as React from 'react';
import { Button, View } from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import {
createStaticNavigation,
useNavigation,
} from '@react-navigation/native';
function HomeScreen() {
const navigation = useNavigation();
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button
onPress={() => navigation.navigate('Notifications')}
title="Go to notifications"
/>
</View>
);
}
function NotificationsScreen() {
const navigation = useNavigation();
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => navigation.goBack()} title="Go back home" />
</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 { Button, View } from 'react-native';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
function HomeScreen() {
const navigation = useNavigation();
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button
onPress={() => navigation.navigate('Notifications')}
title="Go to notifications"
/>
</View>
);
}
function NotificationsScreen() {
const navigation = useNavigation();
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button onPress={() => navigation.goBack()} title="Go back home" />
</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 title="Open drawer" onPress={() => navigation.openDrawer()} />
/* content */
<DrawerItem
label="Close drawer"
onPress={() => props.navigation.closeDrawer()}
/>
<Button title="Open drawer" onPress={() => navigation.openDrawer()} />
/* content */
<DrawerItem
label="Close drawer"
onPress={() => props.navigation.closeDrawer()}
/>
If you would like to toggle the drawer you call the following:
- Static
- Dynamic
<Button title="Toggle drawer" onPress={() => navigation.toggleDrawer()} />
<Button title="Toggle drawer" onPress={() => navigation.toggleDrawer()} />
Each of these functions, behind the scenes, are simply dispatching actions:
- Static
- Dynamic
<Button
title="Open drawer"
onPress={() => navigation.dispatch(DrawerActions.openDrawer())}
/>
/* content */
<DrawerItem
label="Close drawer"
onPress={() => props.navigation.dispatch(DrawerActions.closeDrawer())}
/>
<DrawerItem
label="Toggle drawer"
onPress={() => props.navigation.dispatch(DrawerActions.toggleDrawer())}
/>
<Button
title="Open drawer"
onPress={() => navigation.dispatch(DrawerActions.openDrawer())}
/>
/* 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';