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

Leave a Reply

Your email address will not be published. Required fields are marked *