React useReducer for complex state logic

7750
0

useReducer manages state with reducer patterns like Redux but locally scoped. When state updates depend on previous state or involve multiple sub-values, reducers are clearer than multiple useState calls. The reducer function takes current state and an action, returning new state. Actions are objects with type and optional payload. This pattern centralizes state logic, making it easier to test and reason about. I use useReducer for complex forms, wizards, or mini state machines. Combined with Context, it provides Redux-like architecture without external dependencies. TypeScript discriminated unions ensure type-safe action handling.