Skip to content

Instantly share code, notes, and snippets.

@amandeepmittal
Created September 6, 2018 12:57
Show Gist options
  • Save amandeepmittal/0b49b68c54de870a966f39512dc16dec to your computer and use it in GitHub Desktop.
Save amandeepmittal/0b49b68c54de870a966f39512dc16dec to your computer and use it in GitHub Desktop.

Simple Recipe to Build a Theme App in React Native

Recently, I have been working on a React Native application that demands a changeable theme based on the user selection. I wanted to do this in a simplest way possible. I have so far avoided any React Native UI toolkit such as NativeBase or React Native Elements. In the article below, I am going to share with you all about this simple recipe.

This is what we are building

[GIF 1]

Requirements

You can either clone the application to see how it works from this Github repo here to see everything working properly. Or you can follow the article step by step. It is up to you. To start, generate a react native app using a generator of your choice. We have to choices:

  • react-native cli
  • expo-cli/CRNA

I will use expo-cli for the demo purposes but you can use CRNA(Create-React-Native-Project) if you want. We need to the following dependencies to be installed in our project before we start.

  • redux
  • redux-logger(optional, it will be better for you to debug in case anything goes wrong)
  • react-navigation
  • react-native-vector-icons (only install if you are using react-native-cli)

Getting Started: Navigation

We need only two screen for the demonstration. I am going to call them Home.js and Theme.js. Both are right now contain only boilerplate code. It is good for them to be dumb until we connect them using react-navigation. Create a new file called navigation.js and import both the components.

C1

createStackNavigator is the replacement to StackNavigator in react-navigation version 2+. Notice that, we are not passing a header to the Home Screen.

Creating a Store using Redux

Next step is to build a redux store that will make use of an action, action creator and reducer to change theme in our application. In actions.js put the following code.

C2

We only have one action and changes the theme color of the app. Now the reducer:

C3

Notice that we are setting the primary color in the initial state as the hex value. You can also retrieve it from a configuration file or constants file in your application. We are also exporting the colors.js file which contains three different hex code values that define different color schemes.

C4

Lastly, create the store and it to App.js for the application components to use it.

C5

Inside App.js file we connect our Redux store to manage the state as well as navigation.

C6

You might be wondering why the heck use a Redux Store for such a small application. Well, all I am trying to demonstrate is a pragmatic approach to build a themable application using React Native and Redux. It is your choice to manage the state.

Finalising the App

The last components of our React Native application from where the action happens are Home.js and Theme.js.

C7

By pressing the settings icon, we can navigate to the Themes screen to change background color of the Home screen. We are also passing the title of the Theme Screen here.

https://gist.github.com/7a027309807e3d4a1ba5799489c47897

The magic of changing the color is happening in Home.js at this line:

https://gist.github.com/a6356b6911c73f28a600577f4b82f597

We are receiving the primaryColor from the props. The Theme.js file will be changing the value of this prop on selecting a viable option. Take a look below.

C8

In above, handleThemeChange handles the dispatching of the only action we defined in actions.js. At last, we have our app working.

[Gif 1]

You can find the complete code in this Github repository. 👇

https://github.com/amandeepmittal/30daysofReactNative/tree/master/rn-themes-app

My name is Aman Mittal. I am a developer who builds and consults on Node.js related web apps and ❤️s React Native. This is my website.

If you are unfamiliar on how to use/integrate Redux in a React Native and want learn more about it, you should check the article below.

https://medium.freecodecamp.org/how-to-integrate-redux-into-your-application-with-react-native-and-expo-ec37c9ca6033

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment