Skip to main content

Configuring the header bar

We've seen how to configure the header title already, but let's go over that again before moving on to some other options.

Setting the header title

Each screen has an options property (an object or function returning an object) for configuring the navigator. For the header title, we can use the title option:

const MyStack = createNativeStackNavigator({
screens: {
Home: {
screen: HomeScreen,
options: {
title: 'My home',
},
},
},
});

Header title

Using params in the title

To use params in the title, make options a function that returns a configuration object. React Navigation calls this function with { navigation, route } - so you can use route.params to access the params:

const MyStack = createNativeStackNavigator({
screens: {
Home: {
screen: HomeScreen,
options: {
title: 'My home',
},
},
Profile: {
screen: ProfileScreen,
options: ({ route }) => ({
title: route.params.name,
}),
},
},
});

The argument that is passed in to the options function is an object with the following properties:

We only needed the route object in the above example but you may in some cases want to use navigation as well.

Updating options with setOptions

We can update the header from within a screen using navigation.setOptions:

<Button
onPress={() =>
navigation.setOptions({ title: 'Updated!' })
}
>
Update the title
</Button>

Adjusting header styles

There are three key properties to use when customizing the style of your header:

  • headerStyle: A style object that will be applied to the view that wraps the header. If you set backgroundColor on it, that will be the color of your header.
  • headerTintColor: The back button and title both use this property as their color. In the example below, we set the tint color to white (#fff) so the back button and the header title would be white.
  • headerTitleStyle: If we want to customize the fontFamily, fontWeight and other Text style properties for the title, we can use this to do it.
const MyStack = createNativeStackNavigator({
screens: {
Home: {
screen: HomeScreen,
options: {
title: 'My home',
headerStyle: {
backgroundColor: '#f4511e',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
},
},
},
});