Interview frequency: …
Short answer
Use the useColorScheme hook to listen to the device operating system's theme and connect it to a dynamic color object or Theme Provider to update screen colors immediately.
Explanation
useColorScheme monitors the device appearance in real time and returns "light", "dark", or null. Create a central color palette that selects the right values for the active theme, with an optional user setting to choose a theme manually and store it locally to override the system setting.
Code example
import { useColorScheme, View, Text, StyleSheet } from 'react-native';
function ThemedScreen() {
const theme = useColorScheme() ?? 'light';
const isDark = theme === 'dark';
return (
<View style={[styles.container, { backgroundColor: isDark ? '#121212' : '#ffffff' }]}>
<Text style={{ color: isDark ? '#ffffff' : '#000000' }}>Welcome to the app</Text>
</View>
);
}Common mistakes
- Using the non-reactive Appearance.getColorScheme() value instead of the useColorScheme hook, which listens for immediate changes.