Frontend / Signals and fine-grained reactivity

Signals and fine-grained reactivity

A price, a quantity and a total: see how signals record who reads them, how one write travels to exactly one text node, and how that differs from re-rendering and diffing.

Intermediate7 steps~7 min
Signal — a value that knows who reads it
Computed — a value derived from signals
Effect — code that runs when its inputs change
Page node — the piece of the page an effect writes
// THE SHORT VERSION

In 30 seconds

A signal is a value in a container that knows who reads it. A computed value is derived from signals, and an effect is code, such as a page update, that runs again when the values it read change.

Dependencies are recorded automatically: reading a signal while a computed value or effect runs subscribes it. When a signal is written, only its subscribers run again.

So one text node can be updated without re-running a component or comparing virtual DOM trees.

Signals and fine-grained reactivity, step by step

STEPS01 / 07

API as in Preact Signals (signal, computed, effect, .value); Solid, Vue, Angular and Svelte 5 use other names. Vue and Angular still update per component and use signals to decide which ones.

STEP 01 / 07 A signal is a value in a box
THE MATH
price = signal(10) qty = signal(2)
  1. Step 1: A signal is a value in a box. A signal is a container for one value. Reading and writing go through the container, here with .value, and that is the whole trick: because every read passes through it, a signal can keep a list of who is using its value. Two signals to start: price is 10 and qty is 2.
  2. Step 2: Computed: a derived value. A computed value is worked out from other signals by a function you supply. total multiplies price by qty, giving 20. It behaves like a read-only signal: it keeps its result until one of the signals it used changes, and only then calculates again.
  3. Step 3: Effect: making it visible. An effect is a function that does something in the outside world with signal values, most often changing the page. This one writes the total into a text node. Signals hold data, computed values derive data, and effects act on it: together they form a graph.
  4. Step 4: Dependencies track themselves. Nobody told total that it depends on price and qty. While its function runs, each .value it reads registers total as a subscriber of that signal. The effect subscribes to total in the same way. The dependency graph is built by running the code, and rebuilt each time the code runs again.
  5. Step 5: A write travels the graph. Now qty is set to 3. qty tells its subscriber, total, that its result is out of date; total recalculates 10 × 3 = 30 and tells its own subscriber, the effect, which writes the new text. The update follows the edges of the graph and touches nothing that does not depend on qty.
  6. Step 6: No component re-render. In a fine-grained framework such as Solid, the component function runs once, to create the page elements and set up the effects. After that there is no re-rendering: the effect is wired directly to one text node, so a change to qty rewrites that node and nothing else.
  7. Step 7: Versus virtual DOM diffing. A virtual DOM framework such as React answers the same change differently: it runs the component function again, builds a new description of all seven nodes, compares it with the previous one and writes the single difference. Its work grows with the size of the component; with signals, with what depends on the value.
VALUES SO FAR
price10signal
qty2signal
total····computed
effect····effect
deps····automatic
qty····signal
total····computed
re-runs····component
written····both
// AFTER

Learn next

Nothing builds on this one yet. See every frontend concept.

Spotted a mistake on this page?