Dynamically Change React Navigation Header Title Text StackNavigator

In React Navigation StackNavigator is a major component because it is the base of every activity screen. Every single screen is managed by StackNavigator in react navigation. There are a lot’s of options to customize the StackNavigator is available in react native and one of them is Dynamically Change React Navigation Header Title Text of StackNavigator in react native. We would use react navigation’s setParams() and getParam() method in our this tutorial to modify the Activity title text at application runtime dynamically on button onClick event. So let’s get started .

Content in this project React Native Dynamically Change React Navigation Header Title Text of StackNavigator:

1. Before getting stated the coding part we need to install the React Navigation library in our current project. So open your react native project folder in command prompt or Terminal and execute below command.

Screenshot of CMD:


2. Next step is to install the Gesture Handler library. The React Native Gesture Handler library is used to give the best touch base experience to application users. This library is necessary to install with react native . So again execute below command to install the library.

Screenshot of CMD:

3. Now in final step of installation process we need to manually link both of the libraries to or react native project using link command. So execute the below command in your react native project folder.

4. Now its time to start the coding part. Import StyleSheet, Text, View and TouchableOpacity component in your project’s App.js file.

5. Import createStackNavigator and createAppContainer component from React Navigation library in your current project.

6. Create Static Navigation Options inbuilt function of react navigation in your application main class with Navigation parameter. Now we would return the title text of activity header using navigation.getParam() method. In this method first we would set a key named as title and set some random text to it. This is the Default Title of stack navigator.

7. Create a function named as changeTitleText() in your app’s main class. In this function we would call the navigation.setParams() method with default props of the class. In this function we would simply update the title value.

8. Create a Touchable opacity button in render’s return block and on button onPress event we would call the above function. So when application user click the button it will update the Stack Navigator title text value.

9. Now we need to create a constant Root object and pass the createStackNavigator() in it with our main activity class , We would also call the export default createAppContainer() and pass the Root in it.

10. Creating Style.

11. Complete source code for App.js file class.

Screenshots:

Dynamically Change React Navigation Header Title Text of StackNavigator