A demonstration of global state management using React Context API
π Live Demo - Try it now!
This project showcases how to implement scalable global state management in React applications using the Context API pattern. Built with TypeScript, Vite, and Salt DS, it demonstrates best practices for:
- β¨ Global State Management - Centralized state accessible across components
- π― React Context API - Custom Provider/Consumer pattern implementation
- πͺ Custom Hooks - Clean, reusable hooks for state and actions (
useGetCounterStore,useGetCounterApi) - ποΈ Separation of Concerns - Separated state (store) and actions (API) contexts
- π¦ Type-Safe State - Full TypeScript support for state and actions
The demo features a Counter Application that illustrates:
- Real-time global state updates across components
- Context-based state management without prop drilling
- Beautiful Salt DS components for UI
- Clean architecture with custom hooks pattern
This counter application demonstrates key React Context API patterns:
- Provider Pattern:
CounterStoreProviderwraps the app to provide global state - Multiple Contexts: Separate contexts for state (
CounterStoreContext) and actions (CounterApiContext) - Custom Hooks:
useGetCounterStore()- Access global state from any componentuseGetCounterApi()- Access state actions (increment, decrement, reset)
- No Prop Drilling: Components access state directly via hooks, not through props
- Type Safety: Full TypeScript support ensures type-safe state and actions
- React 19.2 - UI library
- TypeScript 5.9 - Type safety
- Vite 7.x - Fast build tool and dev server
- Salt DS - JPMorgan Chase's design system component library
- Node.js (v18 or higher)
- npm or yarn
npm installStart the development server:
npm run devThe application will be available at http://localhost:5173
Build the application for production:
npm run buildPreview the production build:
npm run previewRun ESLint to check code quality:
npm run lintThis project demonstrates a scalable Context API architecture:
βββ src/
β βββ App.tsx # Main app wrapped with Context Provider
β βββ main.tsx # Entry point with Salt DS provider
β βββ index.css # Global styles
β βββ components/
β βββ CountDisplay/ # Consumer: Reads from global state
β β βββ CountDisplay.tsx # Uses useGetCounterStore() hook
β β βββ index.tsx
β βββ CountController/ # Consumer: Dispatches actions
β β βββ CountController.tsx # Uses useGetCounterApi() hook
β β βββ index.tsx
β βββ CounterContext/ # π― Global State Management Layer
β βββ CounterApiContext.tsx # Context for actions (increment, decrement, reset)
β βββ CounterStoreContext.tsx # Context for state (count value)
β βββ CounterStoreProvider.tsx # Provider component wrapping the app
β βββ use-create-counter-context.ts # Creates state and actions
β βββ use-get-counter-api.ts # Hook to access actions
β βββ use-get-counter-store.ts # Hook to access state
β βββ index.ts
βββ public/ # Static assets
βββ index.html # HTML template
βββ vite.config.ts # Vite configuration
βββ tsconfig.json # TypeScript configuration
This demo implements a dual-context pattern:
- CounterStoreContext - Holds the global state (read-only)
- CounterApiContext - Provides actions to modify state
- CounterStoreProvider - Wraps both contexts for app-wide access
- Custom Hooks - Clean API for components to consume state/actions
- π― Context API Implementation - Production-ready global state pattern
- π Dual-Context Pattern - Separate contexts for state and actions
- πͺ Custom Hooks API - Clean
useGetCounterStore()anduseGetCounterApi()hooks - π« No Prop Drilling - Direct state access from any component
- ποΈ Scalable Architecture - Easy to extend with more state and actions
- π¦ Type-Safe - Full TypeScript support for state and actions
- β‘οΈ Lightning fast development with Vite HMR
- π¨ Salt DS component library integration
- π TypeScript for type safety
- π ESLint for code quality
- π Prettier for consistent code formatting
- π Optimized production builds
The demo counter application showcases Context API patterns:
- State Management: Global state using React Context API
- Custom Hooks:
useGetCounterStore()anduseGetCounterApi()for clean component architecture - Visual Feedback:
- Green color for positive counts
- Red color for negative counts
- Dynamic descriptive text
- User Actions:
- Increment (+1)
- Decrement (-1)
- Reset (back to 0)
- Salt DS Components:
Cardfor elegant containerButtonwith sentiment variants (positive/negative)Display1for large typographyFlowLayoutfor responsive layouts
This demo shows how to implement global state management without external libraries like Redux or Zustand. The Context API pattern demonstrated here is ideal for:
- β Small to medium-sized applications
- β Teams that want to avoid additional dependencies
- β Applications that need centralized state but don't require Redux complexity
- β Learning fundamental React state management concepts
- β Projects that prioritize built-in React features
Salt DS is JPMorgan Chase's open-source design system built specifically for financial applications. It provides a comprehensive set of accessible, well-tested React components that follow design best practices.
Learn more: Salt DS Documentation
This project is configured for automatic deployment to GitHub Pages using GitHub Actions.
π View Live Demo
The demo is automatically deployed whenever changes are pushed to the main branch.
-
Push your code to GitHub:
git add . git commit -m "Add GitHub Pages deployment" git push origin main
-
Enable GitHub Pages:
- Go to your repository on GitHub
- Navigate to Settings β Pages
- Under "Build and deployment":
- Source: Select "GitHub Actions"
- Save the settings
-
Wait for deployment:
- Go to Actions tab in your repository
- Watch the "Deploy to GitHub Pages" workflow run
- Once complete, your app will be live!
-
Update the README:
- Replace
YOUR_USERNAMEin the README with your actual GitHub username - The live demo link will be:
https://YOUR_USERNAME.github.io/react-state-management/
- Replace
You can also trigger a manual deployment:
- Go to Actions tab
- Select "Deploy to GitHub Pages" workflow
- Click "Run workflow"
To preview the production build locally:
npm run build
npm run previewThis project is licensed under the MIT License - see the LICENSE file for details.
