Computed
A cached value derived from other signals.
Create a computed
const count = new Signal(2);
const double = new Computed(() => count.value * 2);
const label = computed(() => `${count.value} items`);
double.value; // 4
A computed reads like a signal but you cannot write it. It recomputes lazily: only when it is read after a signal it depends on changed.
Like a signal, double.peek() reads the current value without
tracking it.
Example
Chains
Computed values can read other computed values. Each one recomputes at most once per change, however many readers it has.
const items = new Signal([3, 8, 1]);
const sorted = computed(() => [...items.value].sort((a, b) => a - b));
const smallest = computed(() => sorted.value[0]);