React native: header styles in navigationOptions not working
I think you are using react navigation version 3. If so navigationOptions is changed to defaultNavigationOptions.
{
initialRouteName: 'Calendar',
defaultNavigationOptions: {
headerStyle: {
backgroundColor: '#28F1A6',
elevation: 0,
shadowOpacity: 0
},
headerTintColor: '#333333',
headerTitleStyle: {
fontWeight: 'bold',
color: '#ffffff'
}
}
}
It should work. https://snack.expo.io/ByGrHdAC7
You need to use defaultNavigationOptions
.
Admittedly, they didn't even mention that they changed it between v2 and v3 in the docs!
https://reactnavigation.org/docs/en/stack-navigator.html
to setup custom react react navigation you need first defined option.navigations or option.defaultNavigationOptions in your App.js
App.js
<Stack.Screen
name="ListeMedocItemScreen"
component={ListeMedocItemScreen}
options={ListeMedocItemScreen.defaultNavigationOptions} // ListeMedocItemScreen.navigationOptions
/>
then in your conponents page
if you use hooks
const ListeMedocItem = (props) =>{
//some component
}
ListeMedocItem.defaultNavigationOptions = ({navigation}) => {
return {
title: 'My home',
headerStyle: {
backgroundColor: '#f4511e',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
}}
or if you use React.Components
static navigationOptions = ({ navigation }) => {
//return header with Custom View which will replace the original header
return {
header: (
<View
style={{
height: 45,
marginTop: 20,
backgroundColor: 'red',
justifyContent: 'center',
}}>
<Text
style={{
color: 'white',
textAlign: 'center',
fontWeight: 'bold',
fontSize: 18,
}}>
This is Custom Header
</Text>
</View>
),
};
};
Just simply change your navigationOptions
to defaultNavigationOptions
and it will work fine
Thankx!