# The Redux Mental Model

> Understand the strict, unidirectional data flow that makes Redux predictable and scalable.

Subject: State Management | Lesson 10 of 22 | 6 min read
Interactive lesson: https://codewithanimation.in/state-management/redux-mental-model
Previous: [Middleware & Persist](https://codewithanimation.in/state-management/zustand-middleware-and-persist.md) | Next: [Redux Toolkit: createSlice & Immer](https://codewithanimation.in/state-management/rtk-createslice-and-immer.md)

## The Single Source of Truth

Redux is a predictable state container. At its core, the Redux mental model relies on a strict **unidirectional data flow** and a single centralized object called the **Store**.

The Store holds the entire application's state tree in one place. Unlike React component state which is scattered and mutable, the Redux Store is the definitive, read-only single source of truth for your app.

## Why enforce unidirectional flow?

In a traditional MVC (Model-View-Controller) architecture, views can update models, and models can update other models, leading to a tangled web of unpredictable state mutations (the 'cascading update' problem).

Redux forces a strict one-way cycle: **State -> UI -> Action -> Reducer -> State**. Data only ever flows in one direction, making it incredibly easy to trace bugs, log state changes over time, and even implement 'time-travel debugging'.

## Actions, Dispatching, and Reducers

Because the state is read-only, you cannot mutate it directly. Instead, you describe state changes using **Actions**.

An Action is a plain JavaScript object with a `type` string describing *what happened* (e.g., `"todos/todoAdded"`). When an event occurs, the UI **dispatches** this action to the Store.

The Store passes the current state and the action into a **Reducer**. A Reducer is a pure function that calculates and returns the brand new state tree. The Store saves this new state and notifies the UI to re-render.

## Common mistake: The 3 Rules of Reducers

Reducers must be strictly pure functions. The most common Redux bug is mutating the existing state directly (e.g., `state.user.name = "Alice"`). If you mutate state directly, Redux won't know the state has changed, and your UI will not re-render.

Reducers must strictly calculate the new state based on arguments, never mutate the arguments, and never run side effects like API calls or timers.

## Tip: Actions are events, not setters

A common anti-pattern is treating actions like setters (`"setCartItems"`, `"setTotal"`). Instead, the official Redux guidelines recommend treating actions as *events that happened* (`"cart/itemAdded"`). This allows multiple different reducers to respond independently to a single event, keeping your logic highly decoupled.

## Try it: The Unidirectional Cycle

Watch the data flow predictably through the Redux architecture. When an action is dispatched, it travels one-way through the reducer to produce a new state tree.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/state-management/redux-mental-model

## Check yourself

### Question 1

Which of the following is strictly forbidden inside a Redux Reducer function?

- A. Copying the existing state to create a new object.
- B. Making an asynchronous API call to fetch data.
- C. Returning the exact same state object if no changes are needed.

Correct answer: B

Reducers must be 100% pure functions. They cannot contain any side effects, meaning no API calls, no timers, and no Math.random(). Asynchronous logic must live outside the reducer.

- Why not A: This is exactly what you are supposed to do in a reducer (immutable updates).
- Why not C: Returning the previous state is standard practice when an action type is not recognized.

## Remember this

- Redux uses a single Store as the single source of truth.
- State is read-only and can only be changed by dispatching an Action.
- Actions are plain objects that describe events that happened.
- Reducers are pure functions that take the current state and action to calculate the next state.
