The Web FrontendDeeper

Kayavak / The Web Frontend / FE·21 UI Architecture / Deeper

Deeper · The Web Frontend

UI Architecture

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Keeping a screen consistent with changing data is an old problem. Trygve Reenskaug described Model–View–Controller at Xerox PARC in 1978–79 for Smalltalk: separate the data from its presentation and from input handling. On the web, the first big single-page apps (Gmail, 2004) did this by hand, and pages filled with code that found elements and patched them; every new feature added more ways for the screen and the data to disagree.

Frameworks attacked that disagreement. Backbone and AngularJS (both 2010) added structure and two-way data binding. React (Facebook, open-sourced in 2013) proposed something more radical: re-describe the whole screen from the current state on every change, and let the framework work out the minimal DOM updates. Its one-way data flow and components spread to almost every framework since. More recent frameworks (Solid, Svelte, Angular, Vue) use signals, which track exactly which values each piece of the screen depends on, an idea with roots in spreadsheets and in earlier reactive libraries such as Knockout.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

UI = f(state). Keep the truth in data, derive the screen from it, and let changes flow down while events flow up. Architecture is deciding where each piece of state lives and who may change it.

Question Answer
What is it? Structuring an interface as components that render from state and communicate through props and events
Problem it solves Keeping many views consistent with changing data as an app and its team grow
Parts Components, props, state and its owners, events and callbacks, design-system primitives, the framework's update mechanism
How it works A change to state schedules re-rendering of the components that depend on it; the framework applies the difference to the DOM
Assumptions Rendering is a pure function of props and state; state has one owner
When they change Duplicated or DOM-held state drifts out of sync; side effects in render run unpredictably
Where it fails Re-rendering too much (performance); state shared too widely (everything depends on everything)
Related ideas Pure functions and immutability (Foundations, ch02); React (ch22); state management (ch25)

What if?

What if the DOM were the source of truth?

Reason it through, then open

To know whether a todo is done you'd read a checkbox; to count items you'd count elements. Any code that changes the DOM (a browser extension, a third-party widget, a translation tool) could change your data, and re-rendering would erase user input you hadn't saved. Keeping truth in JavaScript data and treating the DOM as an output makes the screen reproducible from state.

What if every component could change any state?

Reason it through, then open

You'd lose the ability to answer "why did this change?": any component might have done it. Bugs would require searching the whole app. One-way data flow (state owned in one place, changed only through its owner's callbacks) keeps that question answerable, at the cost of passing callbacks down.

Where the boundary is

Aspect Virtual DOM diffing Signals (fine-grained reactivity)
On a change Re-run the component, compare the new tree with the old Update only what read the changed value
Cost grows with Size of the re-rendered component tree Number of dependents of the value
Mental model Write it as if everything re-renders Values notify their readers
Used by React, Preact Solid, Svelte 5, Angular signals, Vue's reactivity

How sure are we?

Claim How it's known Where from
MVC was described by Reenskaug in 1978–79 Historical His notes from Xerox PARC
React was open-sourced in 2013 Historical JSConf US 2013
Fine-grained reactivity updates less than virtual DOM diffing Measured, conditionally Framework benchmarks; real apps depend more on how state is organised
UI = f(state) Model Holds for rendering; focus, scroll and animations are state that's easy to forget

Still open

The industry is mid-shift from virtual DOM to signals, and a TC39 proposal (2024) would add signals to JavaScript itself. Server-centred approaches (Server Components, htmx-style hypermedia) also question how much UI state belongs in the browser at all.

What would change your mind? If apps built by re-rendering everything matched fine-grained apps' performance without compilers or memoisation, the move to signals would lose its main argument. React's compiler is an attempt at exactly that.

Recall

What does UI = f(state) mean in practice?

Keep truth in data and derive the screen from it; don't read state back from the DOM.

In one-way data flow, which way do data and events travel?

Data flows down through props; events flow up through callbacks to the state's owner.

What's the difference between virtual DOM diffing and signals?

Diffing re-runs components and compares trees; signals update only the parts that read a changed value.

Who described MVC, and when?

Trygve Reenskaug, at Xerox PARC in 1978–79.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.