Skip to main content

DrawerActions reference

DrawerActions is an object containing methods for generating actions specific to drawer-based navigators. Its methods expand upon the actions available in CommonActions.

The following actions are supported:

openDrawer

The openDrawer action can be used to open the drawer pane.

import * as React from 'react';
import { View, Text } from 'react-native';
import { Button } from '@react-navigation/elements';
import {
createStaticNavigation,
useNavigation,
DrawerActions,
} from '@react-navigation/native';
import {
createDrawerNavigator,
DrawerContentScrollView,
DrawerItemList,
DrawerItem,
} from '@react-navigation/drawer';

function HomeScreen() {
const navigation = useNavigation();
const jumpToAction = DrawerActions.jumpTo('Profile', { user: 'Satya' });

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home!</Text>
<Button
onPress={() => {
// codeblock-focus-start
navigation.dispatch(DrawerActions.openDrawer());
// codeblock-focus-end
}}
>
Open Drawer
</Button>
<Button onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}>
Toggle Drawer
</Button>
<Button onPress={() => navigation.dispatch(jumpToAction)}>
Jump to Profile
</Button>
</View>
);
}

function ProfileScreen({ route }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Profile!</Text>
<Text>{route?.params?.user ? route.params.user : 'Noone'}'s profile</Text>
</View>
);
}

function CustomDrawerContent(props) {
return (
<DrawerContentScrollView {...props}>
<DrawerItemList {...props} />
<DrawerItem
label="Close drawer"
onPress={() => props.navigation.dispatch(DrawerActions.closeDrawer())}
/>
<DrawerItem
label="Toggle drawer"
onPress={() => props.navigation.dispatch(DrawerActions.toggleDrawer())}
/>
</DrawerContentScrollView>
);
}

const Drawer = createDrawerNavigator({
drawerContent: (props) => <CustomDrawerContent {...props} />,
screens: {
Home: HomeScreen,
Profile: ProfileScreen,
},
});

const Navigation = createStaticNavigation(Drawer);

export default function App() {
return <Navigation />;
}

closeDrawer

The closeDrawer action can be used to close the drawer pane.

import * as React from 'react';
import { View, Text } from 'react-native';
import { Button } from '@react-navigation/elements';
import {
createStaticNavigation,
useNavigation,
DrawerActions,
} from '@react-navigation/native';
import {
createDrawerNavigator,
DrawerContentScrollView,
DrawerItemList,
DrawerItem,
} from '@react-navigation/drawer';

function HomeScreen() {
const navigation = useNavigation();
const jumpToAction = DrawerActions.jumpTo('Profile', { user: 'Satya' });

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home!</Text>
<Button onPress={() => navigation.dispatch(DrawerActions.openDrawer())}>
Open Drawer
</Button>
<Button onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}>
Toggle Drawer
</Button>
<Button onPress={() => navigation.dispatch(jumpToAction)}>
Jump to Profile
</Button>
</View>
);
}

function ProfileScreen({ route }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Profile!</Text>
<Text>{route?.params?.user ? route.params.user : 'Noone'}'s profile</Text>
</View>
);
}

function CustomDrawerContent({ navigation }) {
return (
<DrawerContentScrollView {...props}>
<DrawerItemList {...props} />
<DrawerItem
label="Close drawer"
onPress={() => {
// codeblock-focus-start
navigation.dispatch(DrawerActions.closeDrawer());
// codeblock-focus-end
}}
/>
<DrawerItem
label="Toggle drawer"
onPress={() => props.navigation.dispatch(DrawerActions.toggleDrawer())}
/>
</DrawerContentScrollView>
);
}

const Drawer = createDrawerNavigator({
drawerContent: (props) => <CustomDrawerContent {...props} />,
screens: {
Home: HomeScreen,
Profile: ProfileScreen,
},
});

const Navigation = createStaticNavigation(Drawer);

export default function App() {
return <Navigation />;
}

toggleDrawer

The toggleDrawer action can be used to open the drawer pane if closed, or close if open.

import * as React from 'react';
import { View, Text } from 'react-native';
import { Button } from '@react-navigation/elements';
import {
createStaticNavigation,
useNavigation,
DrawerActions,
} from '@react-navigation/native';
import {
createDrawerNavigator,
DrawerContentScrollView,
DrawerItemList,
DrawerItem,
} from '@react-navigation/drawer';

function HomeScreen() {
const navigation = useNavigation();
const jumpToAction = DrawerActions.jumpTo('Profile', { user: 'Satya' });

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home!</Text>
<Button onPress={() => navigation.dispatch(DrawerActions.openDrawer())}>
Open Drawer
</Button>
<Button
onPress={() => {
// codeblock-focus-start
navigation.dispatch(DrawerActions.toggleDrawer());
// codeblock-focus-end
}}
>
Toggle Drawer
</Button>
<Button onPress={() => navigation.dispatch(jumpToAction)}>
Jump to Profile
</Button>
</View>
);
}

function ProfileScreen({ route }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Profile!</Text>
<Text>{route?.params?.user ? route.params.user : 'Noone'}'s profile</Text>
</View>
);
}

function CustomDrawerContent(props) {
return (
<DrawerContentScrollView {...props}>
<DrawerItemList {...props} />
<DrawerItem
label="Close drawer"
onPress={() => props.navigation.dispatch(DrawerActions.closeDrawer())}
/>
<DrawerItem
label="Toggle drawer"
onPress={() => props.navigation.dispatch(DrawerActions.toggleDrawer())}
/>
</DrawerContentScrollView>
);
}

const Drawer = createDrawerNavigator({
drawerContent: (props) => <CustomDrawerContent {...props} />,
screens: {
Home: HomeScreen,
Profile: ProfileScreen,
},
});

const Navigation = createStaticNavigation(Drawer);

export default function App() {
return <Navigation />;
}

jumpTo

The jumpTo action can be used to jump to an existing route in the drawer navigator.

  • name - string - Name of the route to jump to.
  • params - object - Screen params to pass to the destination route.
import * as React from 'react';
import { View, Text } from 'react-native';
import { Button } from '@react-navigation/elements';
import {
createStaticNavigation,
useNavigation,
DrawerActions,
} from '@react-navigation/native';
import {
createDrawerNavigator,
DrawerContentScrollView,
DrawerItemList,
DrawerItem,
} from '@react-navigation/drawer';

function HomeScreen() {
const navigation = useNavigation();
const jumpToAction = DrawerActions.jumpTo('Profile', { user: 'Satya' });

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home!</Text>
<Button onPress={() => navigation.dispatch(DrawerActions.openDrawer())}>
Open Drawer
</Button>
<Button onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}>
Toggle Drawer
</Button>
<Button
onPress={() => {
// codeblock-focus-start
navigation.dispatch(jumpToAction);
// codeblock-focus-end
}}
>
Jump to Profile
</Button>
</View>
);
}

function ProfileScreen({ route }) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Profile!</Text>
<Text>{route?.params?.user ? route.params.user : 'Noone'}'s profile</Text>
</View>
);
}

function CustomDrawerContent(props) {
return (
<DrawerContentScrollView {...props}>
<DrawerItemList {...props} />
<DrawerItem
label="Close drawer"
onPress={() => props.navigation.dispatch(DrawerActions.closeDrawer())}
/>
<DrawerItem
label="Toggle drawer"
onPress={() => props.navigation.dispatch(DrawerActions.toggleDrawer())}
/>
</DrawerContentScrollView>
);
}

const Drawer = createDrawerNavigator({
drawerContent: (props) => <CustomDrawerContent {...props} />,
screens: {
Home: HomeScreen,
Profile: ProfileScreen,
},
});

const Navigation = createStaticNavigation(Drawer);

export default function App() {
return <Navigation />;
}