Skip to content
ClapelloNotes by Barath
ReactCh 12 · 2/6
Core4 min readUpdated

React / 65

The Redux loop: action, reducer, store

TL;DR

The UI dispatches an action, a reducer computes the next state, the store saves it, and selectors hand fresh data back to the UI. Always one way.

The loop

1 · ui

User clicks “Add”

onClick={() => dispatch(…)}

2 · dispatch

An action is sent

{ type: "cart/addItem", payload: dosa }

3 · reducer

Next state is computed

(state, action) → newState

4 · store

New state is saved

{ cart: { items: [dosa] } }

5 · selector

Components re-read

state => state.cart.items

6 · ui

Screen updates

Cart (1)

↺ and round again on the next click

Data only ever moves one way round this loop. The UI never edits the store directly: it can only dispatch actions.
WordWhat it isTasty Bites example
StoreThe one object that holds all the shared state{ cart: { items: [] } }
ActionA plain object saying what happened. type is required, payload carries the data{ type: "cart/addItem", payload: dosa }
DispatchThe function you call to send an action to the storedispatch(action)
ReducerA function (state, action) => nextStateadds the dosa to items
SelectorA function that reads one piece of statestate => state.cart.items
SliceOne feature's state plus its reducers (Redux Toolkit's word)the cart slice

"Dispatcher" isn't a separate thing: it just means the dispatch function.

Try it

A tiny Redux store

Try it

1 · ui

Cart (0)

2 · dispatched actions (newest first)

Nothing yet. Press a button.

3 · store state (after the reducer)

{
  "cart": {
    "items": []
  }
}
Each button only dispatches an action object. The reducer decides the next state; the UI just reads it. Add Dosa twice and watch qty go up instead of a duplicate row.

A store is less magic than it sounds

Here is the whole idea in plain JavaScript, no library. The reducer:

cartReducer.js
function cartReducer(state = { items: [] }, action) {
  switch (action.type) {
    case "cart/addItem":
      return { ...state, items: [...state.items, action.payload] };
    case "cart/clearCart":
      return { ...state, items: [] };
    default:
      return state; // an action we don't handle: no change
  }
}

And a tiny store that runs it:

createStore.js
function createStore(reducer) {
  let state = reducer(undefined, { type: "@@init" }); // fills in the default
  const listeners = new Set();
 
  return {
    getState: () => state,
    dispatch(action) {
      state = reducer(state, action);
      listeners.forEach((listener) => listener());
    },
    subscribe(listener) {
      listeners.add(listener);
      return () => listeners.delete(listener);
    },
  };
}
 
const store = createStore(cartReducer);
store.subscribe(() => console.log("items:", store.getState().items.length));
store.dispatch({ type: "cart/addItem", payload: { id: 1, name: "Dosa" } }); // items: 1
store.dispatch({ type: "cart/clearCart" }); // items: 0

Real Redux adds safety checks, middleware and DevTools, but this is the core. react-redux uses subscribe to know when to re-check your components.

The three rules

  1. One source of truth

    All shared state lives in one store, so there's exactly one place to look.
  2. State is read-only

    The only way to change it is to dispatch an action. Components never assign to it.
  3. Reducers are pure

    Same state + same action → same result. No fetch, no Math.random(), no timers, no changing the old object.

Why "don't change the old object"? React-Redux checks for changes with ===. If you edit the old object in place, old and new are the same object, so nothing looks changed and the UI doesn't update.

Don't

Mutating in a plain reducer

case "cart/addItem":
  state.items.push(action.payload);
  return state; // same object: looks unchanged

Do

Return a new object

case "cart/addItem":
  return { ...state, items: [...state.items, action.payload] };

Writing all those ... spreads is tedious. In the next topic, Redux Toolkit lets you write state.items.push(...) safely, and does the copying for you.

Gotchas

Recall

Can you answer these?

0/4

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.