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

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

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