The browser
State → render
Data lives in variables, the screen is always redrawn from it; changes go through the data only.
Why it exists
When data is stored right in the page, every place that shows it has to be updated by hand: the list item, the counter, the title, the badge. Forget one and the screen lies: the counter says "2" while the list has one item. The state → render technique keeps one truth — the state — and the DOM is always derived from it. There's nothing to drift apart, and the same state always gives the same screen.
Where it's used
- React, Vue, Svelte and other UI libraries:
UI = f(state)is their core idea. - Lists with filters and sorting: to-dos, products, emails — an array plus the current filter, and rendering builds the visible part.
- Carts, counters, forms with hints: several places on screen show the same data and must not disagree.
- Debugging: the state can be saved as JSON (say, in localStorage), loaded back — and you see exactly the screen the user saw.
Syntax
let state = …; // the single source of truth
function render() { … } // screen = f(state)
function setState(next) { // the only door for changes
state = next;
render();
}Examples
function view(todos) {
return todos.map((todo) => (todo.done ? "[x] " : "[ ] ") + todo.title);
}
view([
{ title: "Buy cat food", done: true },
{ title: "Take a nap", done: false },
]); // → ["[x] Buy cat food", "[ ] Take a nap"]const state = {
todos: [
{ id: 1, title: "Buy cat food", done: true },
{ id: 2, title: "Take a nap", done: false },
],
filter: "active",
};
function visibleTodos({ todos, filter }) {
if (filter === "active") return todos.filter((todo) => !todo.done);
if (filter === "done") return todos.filter((todo) => todo.done);
return todos;
}
visibleTodos(state).map((todo) => todo.title); // → ["Take a nap"]
visibleTodos({ ...state, filter: "all" }).length; // → 2let todos = [];
let renders = 0;
function render() {
renders += 1;
}
function setTodos(next) {
todos = next;
render();
}
setTodos([...todos, { id: 1, title: "Buy cat food", done: false }]);
setTodos(todos.map((todo) => (todo.id === 1 ? { ...todo, done: true } : todo)));
todos[0].done; // → true
renders; // → 2 — every change redrew the screenResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
The program splits into three parts. State — ordinary variables: the array of to-dos, the current filter. Rendering — a function that reads the state and builds the whole DOM: for example, creates an <li> per to-do and replaces the old content with replaceChildren. Event handlers change the state (preferably with a new array or object) and call rendering.
It helps to have one function for changes — setTodos(next) or setState(next): it stores the new state, saves it to localStorage if needed, and calls render(). Then saving and re-rendering can't be forgotten.
Redrawing everything is simple but not free: re-created elements lose focus and typed text. So input fields are kept outside rendering, or their value is stored in the state. Libraries like React go further: they compare the new result with the old one and change only the difference in the real DOM.
Traps
How it differs from…
- Changing arrays and copies
State is easier to change with new arrays and objects (
[...todos, todo],{ ...todo, done: true }): the old value stays intact, and it's easy to see what changed. React only notices a change by a new reference at all.
When to pick something else
For a tiny page with one button, editing the DOM directly is enough: setting up state and render() for a single label is overkill. And don't redraw huge lists (thousands of elements) entirely on every keystroke: that calls for a library that updates only the difference, or targeted edits.
Practice
Lessons where this appears
- The toy shop windowWorkshop · Chapter 19 · Events
- How a real app is put togetherTale · Chapter 23 · Project: a to-do app
- Why draw the UI from data?Why? · Chapter 23 · Project: a to-do app
- Building the to-do listWorkshop · Chapter 23 · Project: a to-do app
- The case of the vanishing ticksInvestigation · Chapter 23 · Project: a to-do app
- The Living PageBoss · Chapter 23 · Project: a to-do app