Custom Header In React Native & Expo Apps

For Android & iOS Applications

Custom header in react native and expo

Project setup, Package Requirements, and Code

  • Start your expo project
  • When you build a full-fledged application, it has multiple screens. So, we use react-navigation to navigate to the different screens. I am also going to use react-navigation in this project. You are free to use the stack, bottom, drawer, or top tab navigation in your project.
  • Install the following package:
npm install @react-navigation/native
npx expo install react-native-screens react-native-safe-area-context
  • For the stack navigation:
npm install @react-navigation/stack
npx expo install react-native-gesture-handler
  • To finalize the installation of react-native-gesture-handler, add the following at the top of App.js:
import 'react-native-gesture-handler';
  • Optional installation
npx expo install @react-native-masked-view/masked-view
  • Make a screens folder inside the root directory. Inside this directory, make two JavaScript files for the Profile and Home screen.
  • Customize the Profile and Home Screen then import them to the App.js
  • Next, make a header component in a separate JavaScript file, ‘Header.js’.
import { View, Text } from 'react-native'
import React from 'react'

const Header = (props) => {
return (
<View style={{flexDirection:'row', margin:15 }}>
<Text style={{fontWeight:'bold', fontSize:30, color:'white'}}>
{props.name}
</Text>

</View>
)
}

export default Header
  • In the Header.js file, I am using the name of the header as props. The header name for the Home screen will be ‘Home’ and the header name for the Profile screen will be ‘Profile.’
  • Now, in the App.js:
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
component={HomeScreen}
name="HomeScreen"
options={{
headerTitle: () => <Header name="Home" />,
headerStyle: {
backgroundColor: '#4c00b0',
height: 120,
}
}}
/>
<Stack.Screen
component={ProfileScreen}
name="ProfileScreen"
options={{
headerTitle: () => <Header name="Profile" />,
headerStyle: {
backgroundColor: '#4c00b0',
height: 120,
}
}}
/>

</Stack.Navigator>
</NavigationContainer>
  • You can see that I passed both of the screens to the stack screen and inside the headerTitle, I used the props name for Home and Profile Screens.
  • That’s it. Now, you can test the application in the Expo Go Mobile Application.
  • If you are confused or lost, I would recommend watching a step-by-step video tutorial. Few minutes of setup and code, you will be ready to test the application.

Why do I suggest watching more videos?

I am from India and I love writing code and sharing it with the community. We all know that money is the ultimate motivation for work, and the Medium payment system uses Stripe, which has been in preview mode in India for the past 4–5 years. I waited a year on the Medium platform, but in the end, nothing worked out.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store