I am fairly new to react-native. I am trying to set some global header styles for my app, but it's not working
route.js
import React, { Component } from 'react';
import { View } from 'react-native';
import { createStackNavigator, createAppContainer } from "react-navigation";
import SplashScreen from '../screens/SplashScreen';
import CalendarScreeen from '../screens/CalendarScreen';
const NavStack = createStackNavigator(
{
Splash: {
screen: SplashScreen,
navigationOptions: {
header: null
},
},
Calendar: {
screen: CalendarScreeen,
navigationOptions: {
title: 'Calendar',
},
},
},
{
initialRouteName: 'Calendar',
navigationOptions: {
headerStyle: {
backgroundColor: '#28F1A6',
elevation: 0,
shadowOpacity: 0
},
headerTintColor: '#333333',
headerTitleStyle: {
fontWeight: 'bold',
color: '#ffffff'
}
}
}
);
const Routes = createAppContainer(NavStack);
export default Routes;
Now, I am aware that I can do something like this in my class component
static navigationOptions = {
title: 'Chat',
headerStyle: { backgroundColor: 'red' },
headerTitleStyle: { color: 'green' },
}
as it is mentioned in here possible alternative
but I want to achieve the same from my route.js
I also tried defaultNavigationOptions
like its mentioned in docs
But no luck!!
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With