ThemeContext
The React Context that manages theme state throughout the application.
ThemeContext
The ThemeContext
provides theme state management throughout the app, allowing components to access and update the current theme (light or dark mode).
How It Works
ThemeContext is implemented using React's Context API and works with NativeWind's useColorScheme
hook to manage and sync the theme state. The context provides:
- Access to the current theme mode (light/dark)
- A function to toggle between modes
- Automatic application of theme styles through NativeWind
Usage
Accessing Theme State
Conditional Rendering Based on Theme
ThemeProvider Setup
The ThemeProvider
component should wrap your application root to make theme functionality available throughout the app. This is already set up in the _layout.tsx
file:
Implementation Details
The ThemeContext is implemented in app/contexts/ThemeContext.tsx
and includes:
The key elements are:
ThemeContext
: The React context that holds theme stateThemeProvider
: Component that provides the context to the appuseTheme
: Custom hook for easy access to theme state and functions- Integration with NativeWind's
useColorScheme
hook
Advanced Usage
Using with TypeScript
The context provides TypeScript types for better development experience:
Persisting Theme Preference
The current implementation does not persist theme preferences. If you want to add this functionality, you could extend the ThemeContext to use AsyncStorage: