0PricingLogin
React Native Academy · Lesson

A Theme Context with Dark Mode Toggle

Build a ThemeContext that stores the current color scheme, expose a toggle function, and apply theme colors to components throughout the app.

Why a Theme Context?

A theme context centralizes color and typography values so that every component in the app reads from a single source of truth. When the user switches between light and dark mode, updating the context value triggers a re-render of all consuming components with the new colors — no prop drilling or manual updates required.

Defining Light and Dark Theme Objects

Start by defining two theme objects with the same keys. Using identical keys for both themes means every component can apply a color without knowing which mode is active — it just reads from the current theme object and both themes handle the rest.

export const lightTheme = {
  background: '#ffffff',
  text: '#1a1a2e',
  card: '#f5f5f5',
  primary: '#6200ee',
  border: '#e0e0e0',
};

export const darkTheme = {
  background: '#1a1a2e',
  text: '#e8e8f0',
  card: '#16213e',
  primary: '#bb86fc',
  border: '#333355',
};

All lessons in this course

  1. Creating and Providing a Context
  2. Consuming Context with useContext
  3. A Theme Context with Dark Mode Toggle
  4. Avoiding Context Performance Issues
← Back to React Native Academy