JavaScript AcademyProfessional Engineering

Learning object 45 of 100

Application state visualizer

Dispatch actions on a small cart and watch the state object change step by step, with derived values recomputed beside it.

Loading…

What it shows

Application state is the data that decides what the app shows. Keeping it in one object, changing it only through named update functions, and computing derived values such as totals instead of storing them keeps every part of the interface consistent. A store notifies subscribers after each change so the views can re-render.

Guided experiment

  1. Click the buttons in the preview and watch each logged state.
  2. Add the same product twice. Does the line count or the quantity change?
  3. In Layers, follow one click from handler to rule to render.

Predict

Why compute the cart total instead of storing it?

Related

Lessons: Lesson 66: Understanding application state, Lesson 71: Separating data, logic and presentation

Projects: Project 9: Shopping Cart Price Calculator

Source: web/assets/js/objects/object-045.js in the companion repository.