Overview
CryptoDash uses React’s built-in state management solutions:- Context API for global state (theme, settings, notifications)
- Custom Hooks for reusable stateful logic
- Component State for local UI state
Context Providers
SettingsContext
Manages global application settings including theme and language preferences.The SettingsContext automatically persists preferences to localStorage and synchronizes the
dark class on the HTML element.ToastContext
Manages global toast notifications with auto-dismiss functionality.Custom Hooks
CryptoDash includes several powerful custom hooks for reusable logic:useDashboardData
The main hook for fetching and managing dashboard data.- Overview
- Implementation
- Usage
Located at
src/hooks/useDashboardData.js, this hook:- Fetches global crypto statistics
- Loads top market coins
- Manages portfolio data
- Computes derived values (charts, summaries)
- Handles loading and error states
useChartTooltip
Manages interactive chart tooltip state.src/hooks/useChartTooltip.js
useTranslations
Provides internationalization support.src/hooks/useTranslations.js
useDocumentTitle
Dynamically updates page title and meta description.src/hooks/useDocumentTitle.js
Provider Hierarchy
Providers are nested insrc/main.jsx:
src/main.jsx
Best Practices
Context Usage
Context Usage
- Only use Context for truly global state
- Keep contexts focused (separate concerns)
- Provide custom hooks (
useSettings,useToast) for better DX - Throw errors if hooks are used outside providers
Custom Hooks
Custom Hooks
- Start hook names with
use - Return objects for multiple values (better than arrays)
- Use
useCallbackanduseMemoto prevent unnecessary re-renders - Document hook parameters and return values
Performance
Performance
- Use cleanup functions in
useEffectto prevent memory leaks - Implement the
isMountedpattern for async operations - Memoize expensive computations with
useMemo - Memoize callbacks passed to children with
useCallback
Common Patterns
Data Fetching Pattern
LocalStorage Sync Pattern
Next Steps
API Integration
Learn how data is fetched from CoinGecko
Project Structure
Understand the codebase organization
