Dark mode is a standard requirement for modern apps. In React Native, handling it correctly involves more than just swapping colors.
The useColorScheme Hook
React Native provides the useColorScheme hook to detect the user's preference.
import { useColorScheme } from 'react-native';
const App = () => {
const scheme = useColorScheme();
const isDark = scheme === 'dark';
return <View style={isDark ? styles.dark : styles.light}>...</View>;
};
Theming Solutions
For larger apps, manual styling becomes unmanageable. Libraries like Shopify Restyle or Tamagui offer typed theming systems that handle dark mode automatically.
Tips
- Avoid Hardcoded Colors: Always use semantic names like
background,textPrimary, notwhiteorblack. - Test on Device: Simulators don't always reflect the true contrast of OLED screens.