This mini project demonstrates how to manage related and complex state
in React using useReducer instead of multiple useState hooks.
The goal is to understand how state transitions can be centralized and handled through explicit actions.
- useReducer
- Action-based state updates
- State immutability
- Component separation
- Predictable state transitions
- Counter → Displays the current counter value
- Controls → Dispatches actions to update the state
- Reducer → Handles all state transitions in a single place
- History → Stores previous counter values to support undo behavior
useReducer is preferred because:
- Multiple state values are logically related
- State updates depend on previous state
- Actions clearly describe what happened
- State logic is separated from UI components
This makes the application easier to reason about as it grows.
- Scattered state logic across multiple components
- Direct mutation of state inside the reducer
- Using multiple
useStatehooks for tightly coupled state
useReducercentralizes complex state logic- Reducers must always return new state objects
- Actions make state transitions predictable
- Separating state logic from UI improves maintainability