Users choose light or dark mode in their system settings, and some devices move between the two on a schedule. A screen follows that choice only when its colors come from the current scheme at render time. Many apps also let users override the system with their own Light, Dark or System setting, and React Native supports that override directly.

Key terms

  • useColorScheme: a hook that returns the current color scheme and re-renders the component when it changes.
  • Appearance: the module that exposes the user's preferred color scheme and lets the app override it with setColorScheme.
  • PlatformColor: a function that takes one or more native color names and returns the matching native color for use in styles.

The scheme changes while the app runs

useColorScheme reads the color scheme from the Appearance module and subscribes to its updates. On iOS 13 and higher it maps to the user's Dark Mode preference, and on Android 10 (API level 29) and higher to the Dark theme preference. The value can change at any point: the user flips the setting, the app calls Appearance.setColorScheme, or a schedule moves the device between day and night themes.

The hook returns 'light', 'dark' or null. null only comes back when the native Appearance module is unavailable, on out-of-tree platforms, so on iOS and Android a fallback such as useColorScheme() ?? 'light' is there for the type rather than for your users.

Appearance.getColorScheme() returns the same value without subscribing. Code that depends on the scheme should read it on every render rather than caching it, and in components the hook is the way to do that. A palette chosen once at module load goes stale the same way a width cached from Dimensions does.

An in-app theme switch

Appearance.setColorScheme overrides the color scheme for your app only. It doesn't touch the system setting or other apps, and the change applies to the native elements inside your app as well, such as Alerts and Pickers. Pass 'light' or 'dark' to force a scheme, or 'auto' to remove the override and go back to the system preference. After the call, useColorScheme reports the override, or the system value again after 'auto'.

The ThemeSettings screen below offers three rows and maps the System row to 'auto'.

const scheme = useColorScheme() ?? 'light'; runs on every render and indexes the palette object. Tapping Dark calls Appearance.setColorScheme('dark'), the hook returns 'dark', and the screen and row backgrounds switch. Tapping System passes 'auto' and hands the decision back to the device.

Native colors through PlatformColor

PlatformColor takes a native color name and, when the name exists on that platform, returns that native color. Pass several names and the first is the default, with the rest as fallbacks. Native colors respond to the system theme and to high contrast settings, and components that use them pick up the same behavior. In ThemeSettings, the label color comes from the platform instead of the palette.

Names are platform-specific and must match the native string exactly. On iOS they are UIColor names such as label. Android names use the ?attr prefix for theme attributes from R.attr, as in ?android:attr/textColor, and the @android:color prefix for color resources from R.color.

A name from the other platform does not resolve. On Android only names that start with @ or ? can resolve, so PlatformColor('label') matches nothing and the color is left unset. As a text color, the text falls back to its inherited or default color (black) instead of the color you meant, with no warning. As a background color, no background is drawn, and Android writes a ColorValue: Failed to resolve resource paths warning to its native log or raises an error while updating the prop. On iOS an unknown name resolves to a clear color, so the text or background silently disappears. Wrap every PlatformColor call in a Platform.OS check or a Platform.select, as the label style does with its ios, android and default keys.

Code Examples

tsx
import {
  Appearance,
  Platform,
  PlatformColor,
  Pressable,
  StyleSheet,
  Text,
  View,
  useColorScheme,
} from 'react-native';

type ThemeChoice = 'light' | 'dark' | 'auto';

const choices: { value: ThemeChoice; label: string }[] = [
  { value: 'light', label: 'Light' },
  { value: 'dark', label: 'Dark' },
  { value: 'auto', label: 'System' },
];

const palette = {
  light: { screen: '#ffffff', row: '#f2f2f2' },
  dark: { screen: '#121212', row: '#262626' },
};

export function ThemeSettings() {
  const scheme = useColorScheme() ?? 'light';
  const colors = palette[scheme];

  return (
    <View style={[styles.screen, { backgroundColor: colors.screen }]}>
      {choices.map(choice => (
        <Pressable
          key={choice.value}
          onPress={() => Appearance.setColorScheme(choice.value)}
          style={[styles.row, { backgroundColor: colors.row }]}
        >
          <Text style={styles.label}>{choice.label}</Text>
        </Pressable>
      ))}
    </View>
  );
}

const styles = StyleSheet.create({
  screen: { flex: 1, padding: 16 },
  row: { padding: 14, borderRadius: 10, marginBottom: 8 },
  label: {
    fontSize: 17,
    ...Platform.select({
      ios: { color: PlatformColor('label') },
      android: { color: PlatformColor('?android:attr/textColor') },
      default: { color: '#1a1a1a' },
    }),
  },
});
✓ Completed