Navigate back to the homepage

Manage dark mode without installing any package in react native

Infinitbility
React Native
September 4th, 2020 · 1 min read
Manage dark mode without installing any package in react native

Hello Friends,

welcome to Infinitbility!

Today we are going to discuss how to manage dark mode without installing any package in react native with example.

I read many articles about managing dark mode applications in react-native but no one can explain without package. then I write custom code for managing dark mode without any package and sharing with you.

Step 1: Create Global Veriable

Create Global Veriable for manage themeMode on src\App.js

1// App.js
2
3/* App theme mode */
4global.themeMode = 'light'; // light, dark

Step 2: Create a function

Create a function for get and set themeMode from Async Storage

1// functions.js
2
3// call when your app start
4async getThemeMode(){
5 let themeMode = await AsyncStorage.getItem("themeMode");
6
7 if(themeMode) {
8 global.themeMode = themeMode;
9 }else{
10 global.themeMode = 'light';
11 themeMode = 'light';
12 }
13 return themeMode;
14}
15
16// call when user change theme mode
17async setThemeMode(mode){
18 let themeMode = await AsyncStorage.setItem("themeMode", mode);
19 global.themeMode = mode;
20 return mode;
21}

Step 3: Manage Styles file

create seprate const for differant styles on dark mode and light mode.

1// Styles.js
2
3// diffrent styles for modes
4const ThemeStyle = {
5 light: {
6 background: {
7 backgroundColor: '#FFFFFF'
8 }
9 },
10 dark: {
11 background: {
12 backgroundColor: '#000000'
13 }
14 }
15};
16
17// home screen styles
18const HomeScreenStyles = StyleSheet.create({
19 // background
20 content: {
21 flex: 1,
22 },
23});

Step 4: Manage styles on HomeScreen

Here you understand using styles for both mode

1import React from 'react';
2import { View, Text } from 'react-native';
3import { HomeScreenStyles, ThemeStyle } from "../Styles";
4
5
6export default class HomeScreen extends React.Component {
7 constructor(props) {
8 super(props);
9
10 this.state = {
11 loader: false,
12 }
13
14 }
15
16 render() {
17 return (
18 <View style={[HomeScreenStyles.content, ThemeStyle[themeMode].background]}>
19 <Text>Here Your Content</Text>
20 </View>
21 );
22 }
23}

Tips

whenever user change themeMode update your any state such as loader and etc.

if you get help, please share post on your social network

More From React Native Tutorial

Basics

  1. Introduction To React Native
  2. React Native Environment Setup using expo
  3. React Native Environment Setup for windows
  4. React Native Environment setup on Mac OS
  5. React Native Environment setup on linux
  6. React Native Project Structure
  7. React Native State
  8. React Native Props
  9. React Native Styling
  10. React Native Flexbox
  11. React Native Text
  12. React Native Textinput
  13. React Native Commands

Advances

  1. React Native Dark Mode
  2. React Native Fonts
  3. React Native SQLite
  4. React Native DatepickerAndroid
  5. React native ScrollView scroll to position
  6. How to align icon with text in react native
  7. React Native Image
  8. React Native Firebase Crashlytics
  9. React Native Async Storage
  10. React Native Share

Error & Issue Solution

  1. Task :app:transformDexArchiveWithDexMergerForDebug FAILED In React Native
  2. Expiring Daemon because JVM heap space is exhausted In React Native
  3. Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED In React Native
  4. Unable to determine the current character, it is not a string, number, array, or object in react native
  5. App crashed immediately after install react native video or track player
  6. how to delete SQLite database in android react native
  7. React native material dropdown twice click issue
  8. How to get the current route in react-navigation?
  9. how to disable drawer on the drawer navigation screen?
  10. Image not showing in ios 14 react native

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

More articles from Infinitbility

Always Use https url using Cloudflare

Always Use https url using Cloudflare

In this article, we can create page rules for force sites http to https using cloudflare.

September 3rd, 2020 · 1 min read
Deploy React Application on Ubuntu Apache

Deploy React Application on Ubuntu Apache

In this article, we set up a ubuntu server with a react environment, learn the react build process, and figure out some issues like after build getting a blank page, etc.

August 17th, 2020 · 1 min read
© 2020–2021 Infinitbility
Disclaimer
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]