2017-01-26 11:49:39 -08:00
# DrawerNavigator
Used to easily set up a screen with a drawer navigation.
```js
class MyHomeScreen extends React.Component {
static navigationOptions = {
2017-04-13 00:49:08 +02:00
drawerLabel: 'Home',
drawerIcon: ({ tintColor }) => (
< Image
source={require('./chats-icon.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
};
2017-01-26 11:49:39 -08:00
render() {
return (
< Button
onPress={() => this.props.navigation.navigate('Notifications')}
2017-02-08 00:23:44 +05:30
title="Go to notifications"
2017-01-26 11:49:39 -08:00
/>
);
}
}
class MyNotificationsScreen extends React.Component {
static navigationOptions = {
2017-04-13 00:49:08 +02:00
drawerLabel: 'Notifications',
drawerIcon: ({ tintColor }) => (
< Image
source={require('./notif-icon.png')}
style={[styles.tabIcon, {tintColor: tintColor}]}
/>
),
};
2017-01-26 11:49:39 -08:00
render() {
return (
< Button
onPress={() => this.props.navigation.goBack()}
2017-02-08 00:23:44 +05:30
title="Go back home"
2017-01-26 11:49:39 -08:00
/>
);
}
}
const styles = StyleSheet.create({
icon: {
width: 24,
height: 24,
},
});
const MyApp = DrawerNavigator({
Home: {
screen: MyHomeScreen,
},
Notifications: {
screen: MyNotificationsScreen,
},
});
```
To open and close drawer, navigate to `'DrawerOpen'` and `'DrawerClose'` respectively.
```js
this.props.navigation.navigate('DrawerOpen'); // open drawer
this.props.navigation.navigate('DrawerClose'); // close drawer
```
## API Definition
```js
DrawerNavigator(RouteConfigs, DrawerNavigatorConfig)
```
2017-02-02 05:27:05 +08:00
### RouteConfigs
2017-03-22 15:51:16 -05:00
The route configs object is a mapping from route name to a route config, which tells the navigator what to present for that route, see [example ](/docs/api/navigators/StackNavigator.md#routeconfigs ) from `StackNavigator` .
2017-02-02 05:27:05 +08:00
### DrawerNavigatorConfig
2017-01-26 11:49:39 -08:00
- `drawerWidth` - Width of the drawer
2017-01-30 16:44:57 +00:00
- `drawerPosition` - Options are `left` or `right` . Default is `left` position.
2017-04-25 10:53:09 +02:00
- `contentComponent` - Component used to render the content of the drawer, for example, navigation items. Receives the `navigation` prop for the drawer. Defaults to `DrawerItems` . For more information, see below.
2017-01-26 11:49:39 -08:00
- `contentOptions` - Configure the drawer content, see below.
2017-03-30 05:46:29 +02:00
#### Example:
Default the `DrawerView` isn't scrollable.
2017-04-13 00:49:08 +02:00
To achieve a scrollable `View` , you have to use the `contentComponent` to customize the container,
2017-03-30 05:46:29 +02:00
as you can see in the example below.
```js
{
drawerWidth: 200,
drawerPosition: 'right',
2017-04-25 10:53:09 +02:00
contentComponent: props => < ScrollView > < DrawerItems { . . . props } / > < / ScrollView >
2017-03-30 05:46:29 +02:00
}
```
2017-01-26 11:49:39 -08:00
Several options get passed to the underlying router to modify navigation logic:
- `initialRouteName` - The routeName for the initial route.
- `order` - Array of routeNames which defines the order of the drawer items.
- `paths` - Provide a mapping of routeName to path config, which overrides the paths set in the routeConfigs.
- `backBehavior` - Should the back button cause switch to the initial route? If yes, set to `initialRoute` , otherwise `none` . Defaults to `initialRoute` behavior.
2017-03-22 19:09:10 +00:00
### Providing a custom `contentComponent`
You can easily override the default component used by `react-navigation` :
```js
2017-04-25 10:53:09 +02:00
import { DrawerItems } from 'react-navigation';
2017-03-22 19:09:10 +00:00
const CustomDrawerContentComponent = (props) => (
< View style = {style.container} >
2017-04-25 10:53:09 +02:00
< DrawerItems { . . . props } / >
2017-03-22 19:09:10 +00:00
< / View >
);
const styles = StyleSheet.create({
2017-04-13 00:49:08 +02:00
container: {
flex: 1,
2017-03-22 19:09:10 +00:00
},
});
```
2017-04-25 10:53:09 +02:00
### `contentOptions` for `DrawerItems`
2017-01-26 11:49:39 -08:00
- `activeTintColor` - label and icon color of the active label
- `activeBackgroundColor` - background color of the active label
- `inactiveTintColor` - label and icon color of the inactive label
- `inactiveBackgroundColor` - background color of the inactive label
- `style` - style object for the content section
2017-03-30 05:50:56 +02:00
- `labelStyle` - style object to overwrite `Text` style inside content section, when your label is a string
2017-01-26 11:49:39 -08:00
2017-02-02 05:27:05 +08:00
#### Example:
2017-01-26 11:49:39 -08:00
```js
contentOptions: {
activeTintColor: '#e91e63 ',
style: {
marginVertical: 0,
}
}
```
2017-02-23 16:09:11 +01:00
### Screen Navigation Options
2017-04-13 00:49:08 +02:00
#### `title`
2017-02-23 16:09:11 +01:00
2017-04-13 00:49:08 +02:00
Generic title that can be used as a fallback for `headerTitle` and `drawerLabel`
2017-02-23 16:09:11 +01:00
2017-04-13 00:49:08 +02:00
#### `drawerLabel`
2017-02-23 16:09:11 +01:00
2017-04-13 00:49:08 +02:00
String, React Element or a function that given `{ focused: boolean, tintColor: string }` returns a React.Element, to display in drawer sidebar. When undefined, scene `title` is used
2017-02-23 16:09:11 +01:00
2017-04-13 00:49:08 +02:00
#### `drawerIcon`
2017-02-23 16:09:11 +01:00
2017-04-13 00:49:08 +02:00
React Element or a function, that given `{ focused: boolean, tintColor: string }` returns a React.Element, to display in drawer sidebar
2017-02-23 16:09:11 +01:00
2017-01-26 11:49:39 -08:00
### Navigator Props
2017-02-02 05:27:05 +08:00
The navigator component created by `DrawerNavigator(...)` takes the following props:
- `screenProps` - Pass down extra options to child screens, for example:
2017-01-26 11:49:39 -08:00
2017-02-02 05:27:05 +08:00
```jsx
const DrawerNav = DrawerNavigator({
// config
});
2017-03-22 19:09:10 +00:00
2017-02-02 05:27:05 +08:00
< DrawerNav
2017-04-13 00:49:08 +02:00
screenProps={/* this prop will get passed to the screen components and nav options as props.screenProps */}
2017-02-02 05:27:05 +08:00
/>
```
2017-04-23 20:21:15 +02:00
### Nesting `DrawerNavigation`
Please bear in mind that if you nest the DrawerNavigation, the drawer will show below the parent navigation.