Skip to content

Ahmad-shopify-dev/react-state-management

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

React State Management

A comprehensive educational project exploring different state management approaches in React applications.


πŸ“‹ Table of Contents


🎯 Overview

This project demonstrates two powerful state management solutions in React:

  1. React Context API - Built-in React solution for managing application state
  2. Redux Toolkit - Industry-standard state management library with advanced features

Both implementations solve the same problem: managing global state efficiently without prop drilling.


πŸ“ Project Structure

React State Management/
β”œβ”€β”€ React Context API/          # Context API Implementation
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ components/         # React components
β”‚   β”‚   β”‚   β”œβ”€β”€ Dashboard.jsx   # Main dashboard component
β”‚   β”‚   β”‚   └── Login.jsx       # Login component
β”‚   β”‚   β”œβ”€β”€ contexts/
β”‚   β”‚   β”‚   └── UserContext.jsx # User context & provider
β”‚   β”‚   β”œβ”€β”€ App.jsx
β”‚   β”‚   └── main.jsx
β”‚   β”œβ”€β”€ package.json
β”‚   └── vite.config.js
β”‚
└── Redux Toolkit/              # Redux Toolkit Implementation
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ components/         # React components
    β”‚   β”‚   β”œβ”€β”€ KanbanDashboard.jsx  # Kanban dashboard
    β”‚   β”‚   └── Posts.jsx            # Posts management
    β”‚   β”œβ”€β”€ store/
    β”‚   β”‚   β”œβ”€β”€ store.js        # Redux store configuration
    β”‚   β”‚   β”œβ”€β”€ tasksSlice.js   # Tasks reducer slice
    β”‚   β”‚   └── themeSlice.js   # Theme reducer slice
    β”‚   β”œβ”€β”€ services/
    β”‚   β”‚   └── posts.js        # API services
    β”‚   β”œβ”€β”€ App.jsx
    β”‚   └── main.jsx
    β”œβ”€β”€ package.json
    └── vite.config.js

πŸ› οΈ Technologies Used

Common Stack

  • React 18+ - UI library
  • Vite - Build tool and dev server
  • CSS - Styling

React Context API

  • Built-in React Context API
  • useContext Hook
  • useReducer Hook (optional)

Redux Toolkit

  • Redux Core
  • Redux Toolkit
  • Redux Thunk (built-in with Toolkit)
  • React-Redux bindings

πŸš€ Getting Started

Prerequisites

  • Node.js (v14 or higher)
  • npm or yarn package manager

Installation

For React Context API:

cd "React Context API"
npm install
npm run dev

For Redux Toolkit:

cd "Redux Toolkit"
npm install
npm run dev

The applications will run on http://localhost:5173 (default Vite port)


πŸ”„ Project Flow

React Context API Flow

User Action
    ↓
Component Handler
    ↓
Dispatch Action to Context
    ↓
Context Reducer Updates State
    ↓
Context Provider Re-renders
    ↓
Subscribed Components Update
    ↓
UI Reflects Changes

Redux Toolkit Flow

User Action
    ↓
Component Handler
    ↓
Dispatch Redux Action
    ↓
Reducer (Slice) Processes Action
    ↓
Store State Updated
    ↓
Subscribers Notified
    ↓
Connected Components Re-render
    ↓
UI Reflects Changes

❓ Why State Management?

Problems It Solves

  1. Prop Drilling - Avoid passing props through multiple component levels
  2. Component Coupling - Reduce tight coupling between components
  3. Data Consistency - Maintain single source of truth for application state
  4. Debugging - Easier to track state changes and debug applications
  5. Scalability - Manage complex state in large applications efficiently

Without State Management (Prop Drilling)

App 
  β”œβ”€β”€ Component A (passing user prop)
  β”‚   └── Component B (passing user prop)
  β”‚       └── Component C (using user prop) ❌ Cumbersome
  β”‚           └── Component D (using user prop)

With State Management

Global State (User Data)
    ↓
Component A ← directly accesses
Component B ← directly accesses
Component C ← directly accesses
Component D ← directly accesses βœ“ Clean & Efficient

πŸŽ“ Implementation Approaches

React Context API

When to Use:

  • Small to medium-sized applications
  • Simple state management needs
  • Learning React fundamentals
  • Avoiding external dependencies

Advantages:

  • Built into React (no external library)
  • Minimal boilerplate
  • Perfect for beginners
  • Lightweight

Limitations:

  • Performance issues with large frequent updates
  • Limited dev tools
  • Manual optimization needed

Example Usage:

// Create Context
const UserContext = createContext();

// Use in Component
const { user } = useContext(UserContext);

Redux Toolkit

When to Use:

  • Large-scale applications
  • Complex state management
  • Time-travel debugging needed
  • Team collaboration on large projects

Advantages:

  • Powerful dev tools (Redux DevTools)
  • Predictable state management
  • Extensive middleware support
  • Great for large applications
  • Strong community and ecosystem

Limitations:

  • More boilerplate initially
  • Steeper learning curve
  • Extra dependency

Example Usage:

// Create Slice
const tasksSlice = createSlice({
  name: 'tasks',
  initialState,
  reducers: {
    addTask: (state, action) => { /* ... */ }
  }
});

// Use in Component
const dispatch = useDispatch();
const tasks = useSelector(state => state.tasks);

πŸ’‘ How to Use

React Context API Project

  1. Navigate to the project:

    cd "React Context API"
  2. Install dependencies:

    npm install
  3. Start development server:

    npm run dev
  4. Explore the code:

    • Check src/contexts/UserContext.jsx for context setup
    • See src/components/Login.jsx for context usage
    • Review src/components/Dashboard.jsx for state consumption
  5. Build for production:

    npm run build

Redux Toolkit Project

  1. Navigate to the project:

    cd "Redux Toolkit"
  2. Install dependencies:

    npm install
  3. Start development server:

    npm run dev
  4. Explore the code:

    • Check src/store/store.js for store configuration
    • Review src/store/tasksSlice.js and themeSlice.js for reducers
    • See src/components/KanbanDashboard.jsx for Redux usage
    • Examine src/components/Posts.jsx for API integration
  5. Debug with Redux DevTools:

  6. Build for production:

    npm run build

🎯 Key Differences

Feature Context API Redux Toolkit
Learning Curve ⭐ Easy ⭐⭐⭐ Moderate
Boilerplate ⭐ Minimal ⭐⭐ More
Performance ⭐⭐ Good ⭐⭐⭐ Excellent
Dev Tools ⭐ Limited ⭐⭐⭐ Excellent
Scalability ⭐⭐ Medium ⭐⭐⭐ Large
Best For Small Apps Enterprise Apps

πŸ“š Learning Path

  1. Start with React Context API to understand state management basics
  2. Learn about context providers and consumers
  3. Practice with useContext hook
  4. Move to Redux Toolkit for advanced patterns
  5. Understand actions, reducers, and selectors
  6. Explore middleware and side effects
  7. Compare both approaches and choose the right tool for your projects

πŸ”— Useful Resources


πŸ“„ License

This project is licensed under the MIT License - free and open source for educational purposes.

You are free to:

  • βœ… Use this project for learning
  • βœ… Modify and distribute
  • βœ… Use in personal or commercial projects
  • βœ… Include it in your portfolio

See the LICENSE file for more details.


πŸ‘¨β€πŸ’» Author

Created by StackWise Dev

This project is designed for educational purposes to help developers understand React state management patterns and best practices.


🀝 Contributing

This is an educational project. Feel free to fork, modify, and use it for learning purposes!


⭐ Support

If you find this project helpful for learning, consider giving it a star! Happy coding! πŸŽ‰


Last Updated: May 2026
Version: 1.0.0

Releases

No releases published

Packages

 
 
 

Contributors