Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native: header styles in navigationOptions not working

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

like image 935
Nauman Tanwir Avatar asked Nov 30 '18 08:11

Nauman Tanwir


4 Answers

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>
      ),
    };
  };
like image 190
MOUTAIROU Bastou Abdel Avatar answered Oct 23 '22 14:10

MOUTAIROU Bastou Abdel


Just simply change your navigationOptions to defaultNavigationOptions and it will work fine

Thankx!

like image 31
enthusiastdev Avatar answered Oct 23 '22 15:10

enthusiastdev


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

like image 24
Aung Myat Hein Avatar answered Oct 23 '22 15:10

Aung Myat Hein


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

like image 2
Christopher Agnus Lam Avatar answered Oct 23 '22 14:10

Christopher Agnus Lam