Summary: in this tutorial, you will learn how to use the useColorScheme
hook to get the current user-preferred color scheme.
Introduction to the React Native useColorScheme hook
The useColorScheme
hook allows you to get the current user-preferred color scheme.
First, import the useColorScheme
hook from the react-native library:
import {useColorScheme} from 'react-native';
Code language: JavaScript (javascript)
Then, use the useColorScheme
in your functional component to get the current user-preferred color scheme:
const colorScheme = useColorScheme();
Code language: JavaScript (javascript)
The useColorScheme()
hook returns a string or null that indicates the current color scheme. It can be one of three values:
'light'
: light color scheme.'dark'
: dark color scheme.null
: no color scheme has been chosen by the user.
The following example shows how to use the useColorScheme
hook to switch between dark and light themes:
import {useState} from 'react';
import { Text, SafeAreaView, StyleSheet, Switch, useColorScheme } from 'react-native';
export default function App() {
const colorScheme = useColorScheme();
const [isDarkMode, setIsDarkMode] = useState(colorScheme === 'dark');
const theme = isDarkMode ? styles.darkTheme : styles.lightTheme;
return (
<SafeAreaView style={[styles.container, theme.background]}>
<Text style={[styles.text, theme.text]}>JavaScriptTutorial.Net</Text>
<Switch
trackColor={{ false: "#767577", true: "#81b0ff" }}
thumbColor={isDarkMode ? "#f5dd4b" : "#f4f3f4"}
ios_backgroundColor="#3e3e3e"
onValueChange={() => setIsDarkMode(!isDarkMode)}
value={isDarkMode}
/>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 20,
marginBottom: 20,
},
lightTheme: {
background: {
backgroundColor: '#fff',
},
text: {
color: '#000',
},
},
darkTheme: {
background: {
backgroundColor: '#000',
},
text: {
color: '#fff',
},
},
});
Code language: JavaScript (javascript)
Output:
How it works.
First, call the useColorTheme
hook inside the App component:
const colorScheme = useColorScheme();
Code language: JavaScript (javascript)
Second, create a state variable to store a value indicating if the current theme is dark. It defaults to the user-preferred theme:
const [isDarkMode, setIsDarkMode] = useState(colorScheme === 'dark');
Code language: JavaScript (javascript)
Third, assign a style property (dark theme or light theme) to the theme variable based on the value of the isDarkMode
state:L
const theme = isDarkMode ? styles.darkTheme : styles.lightTheme;
Code language: JavaScript (javascript)
Fourth, use the text and background styles in the components based on whether the theme is dark or light. Additionally, create a Switch component to switch between the dark and light theme.
<SafeAreaView style={[styles.container, theme.background]}>
<Text style={[styles.text, theme.text]}>JavaScript Tutorial</Text>
<Switch
trackColor={{ false: "#767577", true: "#81b0ff" }}
thumbColor={isDarkMode ? "#f5dd4b" : "#f4f3f4"}
ios_backgroundColor="#3e3e3e"
onValueChange={() => setIsDarkMode(!isDarkMode)}
value={isDarkMode}
/>
</SafeAreaView>
Code language: JavaScript (javascript)
Finally, define the styles with the dark and light themes:
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 20,
marginBottom: 20,
},
lightTheme: {
background: {
backgroundColor: '#fff',
},
text: {
color: '#000',
},
},
darkTheme: {
background: {
backgroundColor: '#000',
},
text: {
color: '#fff',
},
},
});
Code language: JavaScript (javascript)
Summary
- Use the React Native
useColorScheme
hook to get the current user-preferred color theme.