Learning objective: Connect components to one store so every state change re-renders the views that depend on it, and keep updates predictable with actions.
In state-driven rendering, the interface is always calculated from state. You never edit the page directly in response to a click; you change the state.
A store holds the state. Views subscribe to it and re-render whenever it changes.
Changes are described as actions: small objects such as { type: "add", text: "Milk" }. One function, the reducer, turns the old state and an action into the new state.
Because every change goes through one place, you can log every action and see exactly how the app got into its current state.
A reducer is pure: (state, action) => newState. No DOM, no storage, no time or randomness. That makes it trivially testable and replayable.
Side effects (saving to storage, fetching) run in subscribers or in code around dispatch, never inside the reducer.
Re-render only what depends on the part of state that changed (compare old and new slices) when rendering is expensive; for small apps re-rendering everything is fine.
This is the pattern behind Redux, Vuex/Pinia and React's useReducer. Writing it by hand once makes those libraries much easier to understand.
05Definitions
State-driven rendering
Rendering the interface entirely from state, and changing the interface only by changing state.
Action
An object describing a change, usually with a type and data.
Reducer
A pure function that takes the current state and an action and returns the new state.
Dispatch
Sending an action to the store so the reducer can apply it.
Subscriber
A function the store calls after each change, typically a view's render.
Dynamic list rendering laboratory. Dispatch actions to a list app and watch the reducer produce new state and the list re-render, with an action log you can replay.
Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.
The reducer changes the old state
Return new state instead of mutating.
function cart(state, action) {
if (action.type === "add") {
state.push(action.item);
return state;
}
return state;
}
const before = ["tea"];
const after = cart(before, { type: "add", item: "mug" });
console.log(before.length, after.length, before === after);
A counter reducer
Write a reducer with several actions.
function counter(state, action) {
// TODO: "inc" adds action.by (default 1), "dec" subtracts 1 but never
// below 0, "reset" returns 0; anything else returns state
}
const actions = [{ type: "inc" }, { type: "inc", by: 5 }, { type: "dec" }, { type: "dec" }, { type: "reset" }, { type: "dec" }];
let n = 0;
const seen = [];
for (const a of actions) { n = counter(n, a); seen.push(n); }
console.log(seen.join(" "));
A store-driven shopping list
Wire a reducer, a store and two views.
// reducer(state, action): "add" appends action.text (trimmed, ignored if empty),
// "remove" deletes by index. State shape: { items: [] }
// createStore(reducer, initial) with get, dispatch, subscribe
// Views: #list (li per item) and #summary ("N items")
// The form dispatches "add". Add "Milk", " ", "Eggs", then dispatch
// { type: "remove", index: 0 } and log the list text and summary.
09Prediction
Commit to an answer first, then run the real code to compare.
Drawing and spreadsheet apps keep every change as an action. Undo replays all actions except the last one through the reducer, and collaborative editors send the same actions to other people's browsers to keep everyone in sync.
case "add": state.items.push(action.item); return state;
The reducer mutated and returned the same object, and the view skips unchanged state
dispatch is async
Arrays cannot be state
Views cannot subscribe
4. Two views subscribe; one action is dispatched. How many times does each view render, counting the initial render on subscribe?
Once
Twice
Three times
Never
5. What does an action usually contain?
DOM elements
A type and the data needed for the change
The whole new state
A function
6. A counter appears in the header and the sidebar. Which is more reliable?
Each click handler updates both elements
Both views subscribe to one store and render from state
The sidebar copies the header's text
A timer syncs them
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
The reducer changes the old state: Prints 1 2 false; Unknown actions return the same state
A counter reducer: Prints the sequence; Unknown action
A store-driven shopping list: Prints Eggs 1 items; Reducer is pure; Store state matches
The lesson lives in the companion repository at curriculum/level-03-interactive-web-applications/module-15-interactive-application-architecture/lesson-073-state-driven-rendering/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
The UI is a function of state; change state, not the page.
Actions describe changes; a pure reducer applies them.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationstate-driven rendering
Context: I am learning state-driven rendering in JavaScript.
Task: Explain stores, actions, reducers and subscribers with a small list app.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionstate-driven rendering drill
Context: I have practised state-driven rendering.
Task: Give me six reducers and action lists and ask what the final state is.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingstate-driven rendering bug
Context: My view does not update after I dispatch an action.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationstate-driven rendering practice
Context: I finished the state-driven rendering exercises.
Task: Create three exercises with reducers and connected views.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewstate-driven rendering review
Context: Here is my code that uses state-driven rendering.
Task: Review my store and reducer for mutation and side effects.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.