liveBook · Manning

Signal

As explained in Vanilla Web

A reactive value container that notifies dependent computations and effects when its value changes.

A signal is a value container that notifies dependent computations and effects when its value changes. It is useful when an interface needs derived values and precise updates without a full UI framework. section 13.2.9

Keep a subtotal derived

Create quantity and unit-price signals, then compute the subtotal from their values. An effect reads the subtotal and writes it to an output element. When an input event changes quantity, dependency tracking causes the effect to run again; there is no need to store a second, manually synchronized subtotal. section 13.2.9 section 13.5

Listing 13.4 Derived values and effects with a signals library
import { signal, computed, effect } from "@preact/signals-core";

const quantity = signal(1);
const unitPrice = signal(25);
const subtotal = computed(() => quantity.value * unitPrice.value);

effect(() => {
  subtotalOutput.value = `$${subtotal.value}`;
});

quantityInput.addEventListener("input", () => {
  quantity.value = quantityInput.valueAsNumber || 1;
});

A small @preact/signals-core example connects signal(), computed(), and effect() to a DOM output and an input event. section 13.2.9

Choose signals for fine-grained derived values and effects, not as the default binding pattern. A render function is enough for many interfaces; an EventTarget store fits genuinely shared state. Signals add machinery between data and the DOM, so use a mature library for dependency tracking, cleanup, and batching rather than improvising those mechanics in production. At the time described, browsers have no built-in Signal class. section 13.2.10 section 13.2.9

How Vanilla Web covers it

How deep it goes
Works through signal(), computed(), and effect() in a subtotal binding, but delegates dependency tracking, cleanup, and batching mechanics to a library.
Examples
Uses quantity and unit-price signals to calculate a subtotal that an effect writes to a DOM output after input changes.
What it assumes
Builds on JavaScript input events, state, and DOM updates while introducing signal(), computed(), and effect() without requiring a UI framework.
What you can do after
Apply it via @preact/signals-core to bind a computed subtotal to the DOM without duplicating derived state.
How it connects
Connects signals to derived state, effects, DOM data binding, render functions, EventTarget stores, and Proxy-based state.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.