The reducer pattern rests on three interconnected pillars:
By convention across the JavaScript and React ecosystem (Flux Standard Action pattern), an action object contains:
type (Required): A descriptive string in UPPER_SNAKE_CASE or domain/event format stating what happened.payload (Optional): Any data required to execute the transition (e.g. an ID, an input string, an object).A reducer must adhere strictly to the rules of pure functions:
state in place. Always use object spread (...state) and non-mutating array methods (map, filter).dispatchIn React, the dispatch function returned by useReducer is guaranteed to have a stable identity across all renders.
React guarantees that dispatch will never change its reference between re-renders. This means:
dispatch in useEffect or useCallback dependency arrays.dispatch down through props or Context will never trigger unnecessary child re-renders!fetch() inside a reducer function strictly forbidden?dispatch function returned by useReducer change reference between component re-renders?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| When to Choose useReducer Over useState | Managing Complex State Transitions with useReducer |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.