Redux & Redux Toolkit
/Beginner
Reducers
Definition
Pure functions that take the current state and an action, and return a new state result. `(state, action) => newState`.
Explain Like I'm New
The accountant. The accountant looks at your Current Balance (state), looks at your Deposit Slip (action), does the math in their head, and writes down the New Balance (new state). They NEVER scribble over the old balance, they always write a brand new line.
Real World Example
A massive `switch` statement that looks at the `action.type`. If it says 'LOGOUT', it returns `{ user: null }`. If it says 'LOGIN', it returns `{ user: action.payload }`.
Common Use Cases
- •Calculating state changes securely and predictably
Interactive Example
Loading...
Console output will appear here...
Interview Questions
basic
- Can a Reducer modify the original `state` object passed into it?
intermediate
- Why must a Reducer return the current `state` as a default fallback?