Contents
Introduction
This is an example of React Navigation Header Customization in React Native using Navigation Options. In this example, we will see how to customise the Navigation bar/ Navigation header. React Native does not provide any header by default, it comes when we add React Navigation to switch the activity.
Sometimes we have to customize the header according to the needs and we can easily do it with the help of navigation options. In this post, we will see the full customized header and customization of the right and left header.
Header Title Customisation using
React.useLayoutEffect(() => {
navigation.setOptions({
headerTitle: (props) => (
<Text {...props} style={{color: 'white', fontWeight: 'bold'}}>
Custom Title
</Text>
),
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
});
}, [navigation]);
Left and Right Header Customization using
React.useLayoutEffect(() => {
navigation.setOptions({
title: 'Second Page', //Set Header Title
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
headerTintColor: '#fff', //Set Header text color
headerTitleStyle: {
fontWeight: 'bold', //Set Header text style
},
headerLeft: () => (
<TouchableOpacity
onPress={() => alert('Left Menu Clicked')}
style={{marginLeft: 10}}>
<Text style={{color: 'white'}}>Left Menu</Text>
</TouchableOpacity>
),
headerRight: () => (
<TouchableOpacity
onPress={() => alert('Right Menu Clicked')}
style={{marginRight: 10}}>
<Text style={{color: 'white'}}>Right Menu</Text>
</TouchableOpacity>
),
});
}, [navigation]);
In this example, We will create two screens and we will switch between them using React Navigation. On the first screen, you will see how to add custom title in header and on the second screen, you will see the left and right header customisation.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli
command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next
This will make a project structure with an index file named App.js in your project directory.
Installation of Dependencies
To navigate between screens we need to add react-navigation
and other supporting dependencies.
To install the dependencies open the terminal and jump into your project
cd ProjectName
1. Install react-navigation
npm install @react-navigation/native --save
2. Other supporting libraries react-native-gesture-handler
, react-native-reanimated
, react-native-screens
and react-native-safe-area-context
and @react-native-community/masked-view
npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view --save
3. For the Stack Navigator install
npm install @react-navigation/stack --save
Note: When you use a navigator (such as stack navigator), you’ll need to follow the installation instructions of that navigator for any additional dependencies. If you’re getting an error “Unable to resolve module”, you need to install that module in your project.
You might get warnings related to peer dependencies after installation. They are usually caused by incorrect version ranges specified in some packages. You can safely ignore most warnings as long as your app builds.
CocoaPods Installation
Please use the following command to install CocoaPods
npx pod-install ios
To finalize the installation of react-native-gesture-handler
, add the following at the top (make sure it’s at the top and there’s nothing else before it) of your entry files, such as index.js or App.js:
import 'react-native-gesture-handler';
Note: If you skip this step, your app may crash in production even if it works fine in development.
Project Structure
To start with this Example you need to create a directory named pages in your project and create two files FirstPge.js and SecondPage.js
Code
Now Open App.js in any code editor and replace the code with the following code
App.js
// React Navigation Header Customization in RN using Navigation Options
// https://aboutreact.com/custom-header-using-navigation-options-in-react-native/
import 'react-native-gesture-handler';
import React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import FirstPage from './pages/FirstPage';
import SecondPage from './pages/SecondPage';
const Stack = createStackNavigator();
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="FirstPage">
<Stack.Screen name="FirstPage" component={FirstPage} />
<Stack.Screen name="SecondPage" component={SecondPage} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
Open FirstPage.js in any code editor and replace the code with the following code.
FirstPage.js
// React Navigation Header Customization in RN using Navigation Options
// https://aboutreact.com/custom-header-using-navigation-options-in-react-native/
import React from 'react';
import {
SafeAreaView,
StyleSheet,
Text,
TouchableOpacity,
View,
} from 'react-native';
const FirstPage = ({navigation}) => {
React.useLayoutEffect(() => {
navigation.setOptions({
headerTitle: (props) => (
<Text
{...props}
style={{color: 'white', fontWeight: 'bold'}}>
Custom Title
</Text>
),
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
});
}, [navigation]);
return (
<SafeAreaView style={{flex: 1, backgroundColor: 'white'}}>
<View style={styles.container}>
<Text style={styles.header}>
React Navigation Header Customisation in
RN using Navigation Options
</Text>
<Text>Full Customized Header</Text>
<TouchableOpacity
onPress={() => navigation.navigate('SecondPage')}
activeOpacity={0.7}
style={styles.buttonStyle}>
<Text style={styles.textStyle}>
Click to see Left and Right align header
</Text>
</TouchableOpacity>
</View>
<Text style={{textAlign: 'center', color: 'grey'}}>
www.aboutreact.com
</Text>
</SafeAreaView>
);
};
export default FirstPage;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 16,
},
header: {
fontSize: 25,
textAlign: 'center',
marginVertical: 16,
},
buttonStyle: {
width: '100%',
height: 40,
padding: 10,
backgroundColor: '#808080',
borderRadius: 2,
marginTop: 12,
},
textStyle: {
color: '#fff',
textAlign: 'center',
},
});
Open SecondPage.js in any code editor and replace the code with the following code.
SecondPage.js
// React Navigation Header Customization in RN using Navigation Options
// https://aboutreact.com/custom-header-using-navigation-options-in-react-native/
import React from 'react';
import {
SafeAreaView,
StyleSheet,
View,
Text,
TouchableOpacity,
} from 'react-native';
const SecondPage = ({navigation}) => {
React.useLayoutEffect(() => {
navigation.setOptions({
title: 'Second Page', //Set Header Title
headerStyle: {
backgroundColor: '#f4511e', //Set Header color
},
headerTintColor: '#fff', //Set Header text color
headerTitleStyle: {
fontWeight: 'bold', //Set Header text style
},
headerLeft: () => (
<TouchableOpacity
onPress={() => alert('Left Menu Clicked')}
style={{marginLeft: 10}}>
<Text style={{color: 'white'}}>Left Menu</Text>
</TouchableOpacity>
),
headerRight: () => (
<TouchableOpacity
onPress={() => alert('Right Menu Clicked')}
style={{marginRight: 10}}>
<Text style={{color: 'white'}}>Right Menu</Text>
</TouchableOpacity>
),
});
}, [navigation]);
return (
<SafeAreaView style={{flex: 1, backgroundColor: 'white'}}>
<View style={styles.container}>
<Text style={styles.header}>
React Navigation Header Customisation in
RN using Navigation Options
</Text>
<Text>Left and Right Custom navigation bar</Text>
</View>
<Text style={{textAlign: 'center', color: 'grey'}}>
www.aboutreact.com
</Text>
</SafeAreaView>
);
};
export default SecondPage;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 16,
},
header: {
fontSize: 25,
textAlign: 'center',
marginVertical: 16,
},
textStyle: {
color: '#fff',
textAlign: 'center',
},
});
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running (macOS only)
react-native run-ios
Output Screenshots
Output in Online Emulator
This is how you can Customize Header using Navigation Options in React Native. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
Snehal,
I have created a sepereate js file for cusom header.
I have 5 screens in stack navigation.
All the 5 screens have flat list. this shoould come below my custom header.
But multiple render return not working
any help ?
What do you mean by “multiple render”?