Skip to main content

React Native Configuring Header Bar

The static property of a screen component is called navaigationOptions. It is either an object or a function. It returns an object containing several configuration options.

Props of the header bar

PropsDescription
titleIt sets the title of active screen.
headerStyleIt adds style to header bar.
backgroundColorIt sets the background color of the header bar.
headerTintColorIt sets the color to header title.
headerTitleStyleIt adds style to the title of a screen.
fontWeightIt sets the font style of header title.
  1. static navigationOptions = {  
  2.     title: 'HeaderTitle',  
  3.     headerStyle: {  
  4.         backgroundColor: '#f4511e',  
  5.     },  
  6.     headerTintColor: '#0ff',  
  7.     headerTitleStyle: {  
  8.        fontWeight: 'bold',  
  9.     },  
  10. };  

React Native Moving from One Screen to Other Example 1

In this example, we create two screen named as 'Home' and 'Profile'. The Home screen is set as first screen using "initialRouteName" property and Profile screen as second.
App.js
  1. import React from 'react';  
  2. import { View, Text, Button } from 'react-native';  
  3. import { createStackNavigator, createAppContainer } from 'react-navigation';  
  4.   
  5. class HomeScreen extends React.Component {  
  6.     static navigationOptions = {  
  7.         title: 'Home',  
  8.         headerStyle: {  
  9.             backgroundColor: '#f4511e',  
  10.         },  
  11.         //headerTintColor: '#0ff',  
  12.         headerTitleStyle: {  
  13.             fontWeight: 'bold',  
  14.         },  
  15.     };  
  16.   
  17.     render() {  
  18.         return (  
  19.             <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>  
  20.                 <Text>Home Screen</Text>  
  21.                 <Button  
  22.                     title="Go to Profile"  
  23.                     onPress={() => this.props.navigation.push('Profile')}  
  24.                 />  
  25.             </View>  
  26.         );  
  27.     }  
  28. }  
  29. class ProfileScreen extends React.Component {  
  30.     static navigationOptions = {  
  31.         title: 'Profile',  
  32.         headerStyle: {  
  33.             backgroundColor: '#f4511e',  
  34.         },  
  35.         headerTintColor: '#0ff',  
  36.         headerTitleStyle: {  
  37.             fontWeight: 'bold',  
  38.         },  
  39.     };  
  40.     render() {  
  41.         return (  
  42.             <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>  
  43.                 <Text>Profile Screen</Text>  
  44.                 <Button  
  45.                     title="Go to Profile... again"  
  46.                     onPress={() => this.props.navigation.push('Profile')}  
  47.                 />  
  48.                 <Button  
  49.                     title="Go to Home"  
  50.                     onPress={() => this.props.navigation.navigate('Home')}  
  51.                  />  
  52.                 <Button  
  53.                     title="Go back"  
  54.                     onPress={() => this.props.navigation.goBack()}  
  55.                 />  
  56.             </View>  
  57.     );  
  58.     }  
  59. }  
  60.   
  61. const AppNavigator = createStackNavigator(  
  62.     {  
  63.         Home: HomeScreen,  
  64.         Profile: ProfileScreen  
  65.     },  
  66.     {  
  67.         initialRouteName: "Home"  
  68.     }  
  69. );  
  70.   
  71. const AppContainer = createAppContainer(AppNavigator);  
  72. export default class App extends React.Component {  
  73.     render() {  
  74.         return <AppContainer />;  
  75.     }  
  76. }  

Output:
React Native Configuring Header Bar React Native Configuring Header Bar

Using params in the title

To use the params (parameter) as a title, we need to make navigationOptions as a function which returns a configuration object. Use the this.props inside the navigationOptions. As it is the static property of component "this" does not refer to an instance of componen and therefore no props are available.
Making navigationOptions as a function which returns the object containing {navigation, navigationOptions, screenProps }. The navigation is the object which is passed to screen props as this.props.navigation. We can also get the params from navigation using navigation.getParam or navigation.state.params.

  1. class ProfileScreen extends React.Component {  
  2.     static navigationOptions = ({ navigation }) => {  
  3.         return {  
  4.             title: navigation.getParam('otherParam''A Param Header'),  
  5.         };  
  6.     };  
  7. }  

The navigationOtions configuration of the active screen which can also be updated from the current screen component itself.

  1. //inside render   
  2. <Button  
  3.     title="Update the title"  
  4.     onPress={() => this.props.navigation.setParams({otherParam: 'Header Updated!'})}  
  5. />  

Complete code

In this example, we create two screen "Home" and "Profile". The Profile screen set its header title using params as: title: navigation.getParam('otherParam', 'A Param Header')

App.js

  1. import React from 'react';  
  2. import { View, Text, Button } from 'react-native';  
  3. import { createStackNavigator, createAppContainer } from 'react-navigation';  
  4.   
  5. class HomeScreen extends React.Component {  
  6.     static navigationOptions = {  
  7.         title: 'Home',  
  8.         headerStyle: {  
  9.             backgroundColor: '#f4511e',  
  10.         },  
  11.         //headerTintColor: '#0ff',  
  12.         headerTitleStyle: {  
  13.             fontWeight: 'bold',  
  14.         },  
  15.     };  
  16.   
  17.     render() {  
  18.         return (  
  19.             <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>  
  20.                 <Text>Home Screen</Text>  
  21.                 <Button  
  22.                     title="Go to Profile"  
  23.                     onPress={() => this.props.navigation.push('Profile')}  
  24.                 />  
  25.             </View>  
  26.         );  
  27.     }  
  28. }  
  29. class ProfileScreen extends React.Component {  
  30.     static navigationOptions = ({ navigation }) => {  
  31.         return {  
  32.             title: navigation.getParam('otherParam''A Param Header'),  
  33.         };  
  34.     };  
  35.   
  36.     render() {  
  37.         return (  
  38.             <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>  
  39.                 <Text>Profile Screen</Text>  
  40.                 <Button  
  41.                     title="Go back"  
  42.                     onPress={() => this.props.navigation.goBack()}  
  43.                 />  
  44.                 <Button  
  45.                     title="Update the title"  
  46.                     onPress={() => this.props.navigation.setParams({otherParam: 'Header Updated!'})}  
  47.                 />  
  48.             </View>  
  49.     );  
  50.     }  
  51. }  
  52.   
  53. const AppNavigator = createStackNavigator(  
  54.     {  
  55.         Home: HomeScreen,  
  56.         Profile: ProfileScreen  
  57.     },  
  58.     {  
  59.         initialRouteName: "Home"  
  60.     }  
  61. );  
  62.   
  63. const AppContainer = createAppContainer(AppNavigator);  
  64. export default class App extends React.Component {  
  65.     render() {  
  66.         return <AppContainer />;  
  67.     }  
  68. }  
React Native Configuring Header Bar React Native Configuring Header Bar React Native Configuring Header Bar

Comments

Popular Posts

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...

An introduction to Size Classes for Xcode 8

Introduction to Size Classes for Xcode In iOS 8, Apple introduced  size classes , a way to describe any device in any orientation. Size classes rely heavily on auto layout. Until iOS 8, you could escape auto layout. IN iOS8, Apple changed several UIKit classes to depend on size classes. Modal views, popovers, split views, and image assets directly use size classes to determine how to display an image. Identical code to present a popover on an iPad  causes a iPhone to present a modal view. Different Size Classes There are two sizes for size classes:  compact , and  regular . Sometime you’ll hear about any.  Any  is the generic size that works with anything. The default Xcode layout, is  width:any height:any . This layout is for all cases. The Horizontal and vertical dimensions are called  traits , and can be accessed in code from an instance of  UITraitCollection . The  compact  size descr...

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...