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
