2017-01-26 11:49:39 -08:00
# Configuring the Header
2017-04-03 07:37:47 +06:00
Header is only available for StackNavigator.
2017-01-30 22:17:43 -05:00
In the previous example, we created a StackNavigator to display several screens in our app.
2017-01-26 11:49:39 -08:00
2017-01-30 22:17:43 -05:00
When navigating to a chat screen, we can specify params for the new route by providing them to the navigate function. In this case, we want to provide the name of the person on the chat screen:
2017-01-26 11:49:39 -08:00
```js
this.props.navigation.navigate('Chat', { user: 'Lucy' });
```
2017-01-30 22:17:43 -05:00
The `user` param can be accessed from the chat screen:
2017-01-26 11:49:39 -08:00
```js
class ChatScreen extends React.Component {
render() {
2017-01-31 19:17:50 +03:00
const { params } = this.props.navigation.state;
2017-01-26 11:49:39 -08:00
return < Text > Chat with {params.user}< / Text > ;
}
}
```
### Setting the Header Title
Next, the header title can be configured to use the screen param:
```js
class ChatScreen extends React.Component {
2017-04-13 00:49:08 +02:00
static navigationOptions = ({ navigation }) => ({
title: `Chat with ${navigation.state.params.user}` ,
});
2017-01-26 11:49:39 -08:00
...
}
```
```phone-example
basic-header
```
### Adding a Right Button
Then we can add a [`header` navigation option ](/docs/navigators/navigation-options#Stack-Navigation-Options ) that allows us to add a custom right button:
```js
static navigationOptions = {
2017-04-13 00:49:08 +02:00
headerRight: < Button title = "Info" / > ,
2017-01-26 11:49:39 -08:00
...
```
```phone-example
header-button
```
2017-04-13 00:49:08 +02:00
The navigation options can be defined with a [navigation prop ](/docs/navigators/navigation-prop ). Let's render a different button based on the route params, and set up the button to call `navigation.setParams` when pressed.
2017-01-26 11:49:39 -08:00
```js
2017-04-13 00:49:08 +02:00
static navigationOptions = ({ navigation }) => {
const {state, setParams} = navigation;
const isInfo = state.params.mode === 'info';
const {user} = state.params;
return {
title: isInfo ? `${user}'s Contact Info` : `Chat with ${state.params.user}` ,
headerRight: (
2017-01-26 11:49:39 -08:00
< Button
2017-04-13 00:49:08 +02:00
title={isInfo ? 'Done' : `${user}'s info` }
onPress={() => setParams({ mode: isInfo ? 'none' : 'info'})}
2017-01-26 11:49:39 -08:00
/>
2017-04-13 00:49:08 +02:00
),
};
};
2017-01-26 11:49:39 -08:00
```
Now, the header can interact with the screen route/state:
```phone-example
header-interaction
```
To see the rest of the header options, see the [navigation options document ](/docs/navigators/navigation-options#Stack-Navigation-Options ).