Skip to content

dd-jp/react-state-management

Repository files navigation

React Global State Management Demo

Deploy to GitHub Pages

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

Demo

Counter Application Demo

What This Demo Shows

This counter application demonstrates key React Context API patterns:

  • Provider Pattern: CounterStoreProvider wraps the app to provide global state
  • Multiple Contexts: Separate contexts for state (CounterStoreContext) and actions (CounterApiContext)
  • Custom Hooks:
    • useGetCounterStore() - Access global state from any component
    • useGetCounterApi() - 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

Tech Stack

  • 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

Getting Started

Prerequisites

  • Node.js (v18 or higher)
  • npm or yarn

Installation

npm install

Development

Start the development server:

npm run dev

The application will be available at http://localhost:5173

Build

Build the application for production:

npm run build

Preview

Preview the production build:

npm run preview

Lint

Run ESLint to check code quality:

npm run lint

Project Structure

This 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

Context API Architecture

This demo implements a dual-context pattern:

  1. CounterStoreContext - Holds the global state (read-only)
  2. CounterApiContext - Provides actions to modify state
  3. CounterStoreProvider - Wraps both contexts for app-wide access
  4. Custom Hooks - Clean API for components to consume state/actions

Features

Global State Management

  • 🎯 Context API Implementation - Production-ready global state pattern
  • πŸ”„ Dual-Context Pattern - Separate contexts for state and actions
  • πŸͺ Custom Hooks API - Clean useGetCounterStore() and useGetCounterApi() 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

Development Features

  • ⚑️ 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

Counter Application Features

The demo counter application showcases Context API patterns:

  • State Management: Global state using React Context API
  • Custom Hooks: useGetCounterStore() and useGetCounterApi() 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:
    • Card for elegant container
    • Button with sentiment variants (positive/negative)
    • Display1 for large typography
    • FlowLayout for responsive layouts

Why This Pattern?

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

About Salt DS

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

Deployment

This project is configured for automatic deployment to GitHub Pages using GitHub Actions.

Live Demo

πŸš€ View Live Demo

The demo is automatically deployed whenever changes are pushed to the main branch.

Setting Up GitHub Pages (First Time)

  1. Push your code to GitHub:

    git add .
    git commit -m "Add GitHub Pages deployment"
    git push origin main
  2. Enable GitHub Pages:

    • Go to your repository on GitHub
    • Navigate to Settings β†’ Pages
    • Under "Build and deployment":
      • Source: Select "GitHub Actions"
    • Save the settings
  3. 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!
  4. Update the README:

    • Replace YOUR_USERNAME in the README with your actual GitHub username
    • The live demo link will be: https://YOUR_USERNAME.github.io/react-state-management/

Manual Deployment

You can also trigger a manual deployment:

  • Go to Actions tab
  • Select "Deploy to GitHub Pages" workflow
  • Click "Run workflow"

Local Preview of Production Build

To preview the production build locally:

npm run build
npm run preview

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Global state management with Context

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •