You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/headers.md
+69-2Lines changed: 69 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,8 +94,74 @@ There are a couple of things to notice here:
94
94
1. On iOS the status bar is black, and this doesn't look great over a dark colored background. We won't discuss it here, but you should be sure to configure the status bar to fit with your screen colors [as described in the status bar guide](status-bar.html).
95
95
2. The configuration we set only applies to the home screen; when we navigate to the details screen, the default styles are back. We'll look at how to share `navigationOptions` between screens now.
96
96
97
-
## Sharing `navigationOptions` across screens
97
+
## Sharing common `navigationOptions` across screens
98
98
99
+
It is common to want to configure the header in a similar way across many screen. For example, your company brand color might be red and so you want the header background color to be red and tint color to be white. Conveniently, these are the colors we're using our running example, and you'll notice that when you navigate to the `DetailsScreen` the colors go back to the defaults. Wouldn't it be awful if we had to copy the `navigationOptions` header style properties from `HomeScreen` to `DetailsScreen`, and for every single screen component we use in our app? Thankfully, we do not. We can instead move the configuration up to the `StackNavigator`.
100
+
101
+
```js
102
+
classHomeScreenextendsReact.Component {
103
+
static navigationOptions = {
104
+
title:'Home',
105
+
/* No more header config here! */
106
+
};
107
+
108
+
/* render function, etc */
109
+
}
110
+
111
+
/* other code... */
112
+
113
+
constRootStack=StackNavigator(
114
+
{
115
+
Home: {
116
+
screen: HomeScreen,
117
+
},
118
+
Details: {
119
+
screen: DetailsScreen,
120
+
},
121
+
},
122
+
{
123
+
initialRouteName:'Home',
124
+
/* The header config from HomeScreen is now here */
125
+
navigationOptions: {
126
+
headerStyle: {
127
+
backgroundColor:'#f4511e',
128
+
},
129
+
headerTintColor:'#fff',
130
+
headerTitleStyle: {
131
+
fontWeight:'bold',
132
+
},
133
+
},
134
+
}
135
+
);
136
+
```
137
+
<ahref="https://snack.expo.io/@react-navigation/sharing-header-styles"target="blank"class="run-code-button">→ Run this code</a>
138
+
139
+
Now, any screen that belongs to the `RouteStack` will have our wonderful branded styles. Surely though, there must be a way to override these options if we need to?
140
+
141
+
## Overriding shared `navigationOptions`
142
+
143
+
The `navigationOptions` specified on your screen component are merged together with those of its parent `StackNavigator`, with the options on the screen component taking precedence. Let's use this knowledge to invert the background and tint colors on the details screen.
/* These values are used instead of the shared configuration! */
154
+
headerStyle: {
155
+
backgroundColor:'#fff',
156
+
},
157
+
headerTintColor:'#f4511e',
158
+
};
159
+
};
160
+
161
+
/* render function, etc */
162
+
}
163
+
```
164
+
<ahref="https://snack.expo.io/@react-navigation/overriding-shared-header-styles"target="blank"class="run-code-button">→ Run this code</a>
99
165
100
166
## Using a different component for the title
101
167
@@ -110,9 +176,10 @@ class ChatScreen extends React.Component {
110
176
}
111
177
```
112
178
179
+
113
180
## Additional configuration
114
181
115
-
You can read the full list of available screen `navigationOptions` in the [StackNavigator reference](stack-navigator.html#screen-navigation-options).
182
+
You can read the full list of available screen `navigationOptions`for screens inside of `StackNavigator`in the [StackNavigator reference](stack-navigator.html#screen-navigation-options).
116
183
117
184
<!-- Each screen can configure various aspects about how it gets presented in parent navigators. You can configure
0 commit comments