Skip to main content

Lesson 7 of 9

useContext - Theme & Preferences

Context is the right tool when the same value is needed by many components at different nesting levels. Theme (colors, fonts, spacing) and user preferences are the canonical use cases. Avoiding context re-renders requires splitting context by update frequency.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Theme Context Pattern

Theme Context Pattern
import { createContext, useContext, useState } from 'react';

// ---- Theme definition ----
const light = {
  background: '#f8fafc', surface: '#ffffff', text: '#0f172a', textSecondary: '#64748b',
  primary: '#229c6b', border: '#e2e8f0',
} as const;

const dark = {
  background: '#0f172a', surface: '#1e293b', text: '#f1f5f9', textSecondary: '#94a3b8',
  primary: '#818cf8', border: '#334155',
} as const;

type Theme = typeof dark;
type ColorScheme = 'light' | 'dark';

interface ThemeContextValue {
  theme: Theme;
  scheme: ColorScheme;
  toggle: () => void;
}

const ThemeContext = createContext<ThemeContextValue | null>(null);

export function ThemeProvider({ children }: { children: React.ReactNode }) {
  const [scheme, setScheme] = useState<ColorScheme>('dark');
  const theme = scheme === 'dark' ? dark : light;
  const toggle = () => setScheme(s => s === 'dark' ? 'light' : 'dark');

  // useMemo prevents creating new context value object every render
  const value = useMemo(() => ({ theme, scheme, toggle }), [theme, scheme]);

  return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
}

export function useTheme() {
  const ctx = useContext(ThemeContext);
  if (!ctx) throw new Error('useTheme must be inside ThemeProvider');
  return ctx;
}

// Usage - any component, any depth
function Card({ title }: { title: string }) {
  const { theme } = useTheme();
  return (
    <View style={{ backgroundColor: theme.surface, padding: 16, borderRadius: 10, borderWidth: 1, borderColor: theme.border }}>
      <Text style={{ color: theme.text, fontSize: 16, fontWeight: '700' }}>{title}</Text>
    </View>
  );
}

function ThemeToggle() {
  const { scheme, toggle } = useTheme();
  return (
    <Pressable onPress={toggle} style={{ padding: 8 }} accessibilityRole="button" accessibilityLabel="Toggle theme">
      <Text style={{ fontSize: 20 }}>{scheme === 'dark' ? '☀️' : '🌙'}</Text>
    </Pressable>
  );
}

Tip

Tip

Practice useContext Theme Preferences in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Injecting dynamic dark/light theme palettes across the entire app tree

Practice Task

Note

Practice Task - (1) Write a working example of useContext Theme Preferences from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with useContext Theme Preferences is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.

Topics in This Module

0/9 done