React Navigation 5.x Change Header Title on Button Click in React Native Dynamically

In react navigation 5.x there are options available to set the app screen Title text. There are sometimes developer is required to update the Title on active screen using navigation.setOptions({}) method. This method would allow us the update the active screen style on button click events. In navigation.setOptions() there are a prop named as title which is used to update the header title. So in this tutorial we would learn about React Navigation 5.x Change Header Title on Button Click in React Native Dynamically Android iOS Example Tutorial.

Contents in this project React Navigation 5.x Change Header Title on Button Click in React Native Dynamically Android iOS Example:

1. The first step before start coding is to download and install the React Navigation and its supporting libraries in our current react native project. I have already make a post on this topic containing installation guide. Here is the link of my React Navigation 5.x installation guide. Visit the post and follow Step 1, 2 and 3.

2. Open your project’s main App.js file and import StyleSheet, Text, View, Button, NavigationContainer, createStackNavigator and react-native-gesture-handler component.

3. Create a functional component named as Home with navigation prop argument. Here we would use the navigation to control the navigation options like Title and update the title text using navigations.

  • navigation.setOptions({ title: ‘Updated!’ } : Is used to Change the Title text dynamically on app run time on button click.

4. Making a createStackNavigator() object named as Stack and here we would also define Stack.Screen wrap inside NavigationContainer -> Stack.Navigator.

5. Creating Style.

6. Now in the final step we have to call the export default method which would call this Stack on app start time.

7. Complete Source Code for App.js file class:

Screenshots:

React Navigation 5.x Change Header Title on Button Click in React Native Dynamically

Leave a Reply

Your email address will not be published.