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

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

16 AWS Gotchas

16 AWS Gotchas In January I launched the MVP for my own startup,  Proximistyle , which helps you find what you’re looking for nearby. On advice from friends and industry contacts I chose AWS as my cloud provider. Having never had to set up my own cloud infrastructure before, the learning curve to get from no experience to a stable VPC system I was happy with was significantly steeper than expected, and had its fair share of surprises. #1 Take advantage of the free resources offered AWS offers a free tier for new accounts. If you have recently bought a domain and set up a company you qualify for the free tier for a year. Additionally, if you are a bootstrapped startup you can apply for  the Startup Builders package  and get $1000 in AWS credits. After doing the above, you’re now ready to get started with setting up the AWS infrastructure for your startup. #2 Set up billing budgets and alerting The very first thing you should do after setting up billing, is enabling a budge...

Ultimate Folder Structure For Your React Native Project

  Ultimate Folder Structure For Your React Native Project React native project structure React Native is a flexible framework, giving developers the freedom to choose their code structure. However, this can be a double-edged sword for beginners. Though it offers ease of coding, it can soon become challenging to manage as your project expands. Thus, a structured folder system can be beneficial in many ways like better organization, simplified module management, adhering to coding practices, and giving a professional touch to your project. This write-up discusses a version of a folder arrangement that I employ in my React Native projects. This structure is based on best practices and can be modified to suit the specific needs of your project. Before we get into the project structure let’s give credit to @sanjay who has the original idea of the structure but I modify his version of the code, to make it better. Base library axios  — For network calling. react-navigation ...