React / 65
The Redux loop: action, reducer, store
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) → newState4 · store
New state is saved
{ cart: { items: [dosa] } }5 · selector
Components re-read
state => state.cart.items6 · ui
Screen updates
↺ and round again on the next click
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) → newState6 · ui
Screen updates
5 · selector
Components re-read
state => state.cart.items4 · store
New state is saved
{ cart: { items: [dosa] } }| Word | What it is | Tasty Bites example |
|---|---|---|
| Store | The one object that holds all the shared state | { cart: { items: [] } } |
| Action | A plain object saying what happened. type is required, payload carries the data | { type: "cart/addItem", payload: dosa } |
| Dispatch | The function you call to send an action to the store | dispatch(action) |
| Reducer | A function (state, action) => nextState | adds the dosa to items |
| Selector | A function that reads one piece of state | state => state.cart.items |
| Slice | One 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 it1 · ui
Cart (0)2 · dispatched actions (newest first)
Nothing yet. Press a button.
3 · store state (after the reducer)
{
"cart": {
"items": []
}
}A store is less magic than it sounds
Here is the whole idea in plain JavaScript, no library. The reducer:
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:
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: 0Real 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
One source of truth
All shared state lives in one store, so there's exactly one place to look.State is read-only
The only way to change it is to dispatch an action. Components never assign to it.Reducers are pure
Same state + same action → same result. Nofetch, noMath.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 unchangedDo
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.
