React Native Dynamically Change Static navigationOptions Value on Button Click iOS Android

React Native activity(React Navigation) structure has its own Static navigationOptions, which is used set Activity action bar background color, title text color and title text itself and some style options. But the both are by default set only statically once in react native application and sometimes application developer needs to update them dynamically. So in this tutorial we would going to Dynamically Change Static navigationOptions Value on Button Click in iOS Android React Native application using this.props.navigation.setParams() inbuilt method. So let’s get started 🙂 .

What we are doing in this project:

We would dynamically change the Static navigationOptions using this.props.navigation.setParams() method and change the Activity action bar background color, Text and Text color on button onPress event.

Contents in this project Dynamically Change Static navigationOptions Value on Button Click in iOS Android React Native App:

1. Before going further we need to install the React Navigation Library in our react native project in order to use the Activity structure. So open your project folder in Command Prompt(Terminal) and execute the below command.

Screenshot after done installing the library:

2. Import StyleSheet, View, Text, YellowBox and TouchableOpacity component in your project.

3. Import StackNavigator component in your project from React Navigation library.

4. We are using only 1 Activity in our project, So create a class named as MainActivity.

5. Create constructor() in your MainActivity class and write the YellowBox.ignoreWarnings() code to remove the isMounted(…) is deprecated yellow page error.

6. Create static navigationOptions with navigation prop in your MainActivity class. We would use the navigation.getParam() with object to set their values dynamically. If we do not change the value then it will by default call the Right side value as default value and the First object value we can change from function.

7. Create function named as apply_Green(), Inside this function we would dynamically update the static navigationOptions using this.props.navigation.setParams(). We would update both 3 values of Title, background color and Header tint color.

8. Create another function named as apply_Sky_Blue(), Inside this function we would done the same as above function.

9. Create another function named as apply_Orange(), Inside this function we would also update all the values.

10. Create 3 Buttons using TouchableOpacity component inside render’s return block and call the above 3 functions on button onPress event.

11. Create StackNavigator activity calling structure and call the MainActivity inside it. This step is very important.

12. Create CSS Style class.

13. Complete source code for App.js File :

Screenshots:

Dynamically Change Static navigationOptions Value

4 Comments

  1. Thanks to you, I have completed a demand on my project.
    Thank you so much!

  2. I copied exactly code and failed

Leave a Reply

Your email address will not be published. Required fields are marked *