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
// App.js
/* App theme mode */
global.themeMode = 'light'; // light, dark
Step 2: Create a function
Create a function for get and set themeMode from Async Storage
// functions.js
// call when your app start
async getThemeMode(){
let themeMode = await AsyncStorage.getItem("themeMode");
if(themeMode) {
global.themeMode = themeMode;
}else{
global.themeMode = 'light';
themeMode = 'light';
}
return themeMode;
}
// call when user change theme mode
async setThemeMode(mode){
let themeMode = await AsyncStorage.setItem("themeMode", mode);
global.themeMode = mode;
return mode;
}
Step 3: Manage Styles file
create seprate const for differant styles on dark mode and light mode.
// Styles.js
// diffrent styles for modes
const ThemeStyle = {
light: {
background: {
backgroundColor: '#FFFFFF'
}
},
dark: {
background: {
backgroundColor: '#000000'
}
}
};
// home screen styles
const HomeScreenStyles = StyleSheet.create({
// background
content: {
flex: 1,
},
});
Step 4: Manage styles on HomeScreen
Here you understand using styles for both mode
import React from 'react';
import { View, Text } from 'react-native';
import { HomeScreenStyles, ThemeStyle } from "../Styles";
export default class HomeScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
loader: false,
}
}
render() {
return (
<View style={[HomeScreenStyles.content, ThemeStyle[themeMode].background]}>
<Text>Here Your Content</Text>
</View>
);
}
}
Tips
whenever user change themeMode update your any state such as loader and etc.
Thanks for reading…
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
Advances
4. React Native DatepickerAndroid
5. React native ScrollView scroll to position
6. How to align icon with text in react native
8. React Native Firebase Crashlytics
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
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
11. React Native image picker launchimagelibrary on second time issue
12. how to open any link from react native render Html