Computed Signals
What you'll learn: How to derive new signals from existing ones that automatically update when their dependencies change.
The Core Idea
Computed signals are derived values that automatically recalculate whenever the signals they depend on change. Think of them like spreadsheet formulas: when cell A1 or B1 changes, the formula in C1 (=A1+B1) instantly updates without you manually recalculating.
Instead of manually tracking which signals affect which values, computed signals establish declarative relationships between data. You describe what the value should be based on other signals, and the framework handles when to recalculate.
How They Work
A computed signal wraps a function that reads one or more other signals. The system tracks which signals you access during that function's execution, creating automatic dependencies. Whenever any dependency changes, the computed signal marks itself as "dirty" and recalculates on the next read.
const price = signal(10);
const quantity = signal(5);
// Computed signal automatically tracks dependencies
const total = computed(() => price() * quantity());
console.log(total()); // 50
price(12);
console.log(total()); // 60 — automatically updated!
quantity(3);
console.log(total()); // 36 — tracks both dependencies
The beauty is you never told total to watch price or quantity — it discovered those dependencies by observing what you read inside the computation function.
Why This Matters
Computed signals eliminate manual synchronization code. No more remembering to update total every time price or quantity changes. The data flow becomes declarative: you state the relationship once, and it maintains itself automatically.
Key Takeaway: Computed signals create self-maintaining derived values by automatically tracking which signals they read, recalculating only when dependencies change, forming the foundation of reactive data flows.