React native: header styles in navigationOptions not working

Nauman Tanwir picture Nauman Tanwir · Nov 30, 2018 · Viewed 15.6k times · Source

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!!

Answer

Aung Myat Hein picture Aung Myat Hein · Nov 30, 2018

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