Overview
TheSettingsContext provides a centralized way to manage user settings across your CryptoDash application. It handles theme (dark/light mode) and language (English/Spanish) preferences with automatic persistence to localStorage.
Components
SettingsProvider
The context provider component that wraps your application to provide settings state and actions.The child components that will have access to the settings context
Example: App Setup
Hook API
useSettings
A custom hook that provides access to settings state and actions. Must be used within aSettingsProvider.
Returns: Settings context object with the following properties:
Current theme setting. Defaults to ‘dark’
Function to directly set the theme
Function to toggle between dark and light themes
Current language setting. Defaults to ‘en’ (English)
Function to directly set the language
Function to toggle between English and Spanish
Usage Examples
Basic Usage
Theme Toggle
Language Selector
Direct Theme Setting
Features
Automatic Persistence
Settings are automatically persisted to localStorage:- Theme: stored in
crypto-dash-theme - Language: stored in
crypto-dash-language
Dark Mode Class Management
The provider automatically manages thedark class on the document element:
- Applies
darkclass when theme is ‘dark’ - Removes
darkclass when theme is ‘light’
Error Handling
The hook throws an error if used outside of aSettingsProvider:
Implementation Details
The context uses React hooks internally:useStatewith lazy initialization from localStorageuseEffectto sync changes to localStorage and DOM- Initial theme is applied on mount to prevent flash of wrong theme
Related
- useTranslations Hook - Consumes the language setting
- ToastContext - Often used together for user feedback
