Svelte Reactive Statements
A reactive statement runs again each time one of its inputs changes. Svelte marks these statements with a dollar sign and a colon. Three shapes exist: a reactive declaration, a reactive block, and a reactive condition.
Reactive Declarations
A reactive declaration creates a variable whose value always follows other variables. Svelte reads the right side, finds the variables it uses, and recalculates the result after any of them changes.
<script>
let celsius = 25;
$: fahrenheit = celsius * 9 / 5 + 32;
</script>
<input type="number" bind:value={celsius} />
<p>{celsius} C equals {fahrenheit} F</p>The variable fahrenheit needs no let keyword. The dollar colon label declares it automatically.
Dependency Diagram
price quantity taxRate
\ | /
\ | /
v v v
$: subtotal = price * quantity
|
v
$: total = subtotal + subtotal * taxRate
|
v
Page shows totalChanging quantity recalculates subtotal first and total second. Changing taxRate recalculates only total, since subtotal does not depend on it.
Reactive Blocks
Curly braces group several lines under one label. The whole block reruns when any variable used inside it changes.
<script>
let items = 3;
let price = 12;
let summary = "";
$: {
const total = items * price;
summary = `Cart holds ${items} items worth ${total}`;
console.log(summary);
}
</script>Reactive Conditions
An if statement after the label runs only when its inputs change. The condition decides whether the inner code executes.
<script>
let score = 0;
let message = "Keep going";
$: if (score >= 10) {
message = "Level complete";
}
</script>
<button on:click={() => score += 1}>Add Point</button>
<p>{message}</p>Execution Order
Svelte sorts reactive statements by their dependencies, so a statement that needs a value runs after the statement that produces it. Write them in a natural top-to-bottom order. Readers follow the logic faster when the order matches the flow.
Reactive statements run once after the script finishes setting up, and again after each relevant change. Several assignments inside one event handler trigger one batch of updates, not one update per assignment.
A Layman Analogy
Picture a spreadsheet. Cell C1 holds the formula A1 plus B1. Typing a new number into A1 updates C1 instantly, and you never press a refresh button. A reactive declaration gives your variables the same formula behavior.
The Hidden Dependency Trap
Svelte finds dependencies by reading the statement itself. Variables used inside a called function stay invisible to this scan.
<script>
let width = 5;
let height = 4;
function calcArea() {
return width * height;
}
// Does not update: width and height are hidden inside the function
$: area = calcArea();
// Updates correctly: variables appear in the statement
$: areaFixed = calcArea2(width, height);
function calcArea2(w, h) {
return w * h;
}
</script>Pass the values as arguments, and Svelte sees them. Svelte 5 offers runes that replace these labels, and the Svelte Runes topic covers that approach.
Common Mistakes To Avoid
- Hiding dependencies inside functions
- Assigning to a variable that the same statement reads, which creates a loop
- Using reactive blocks for work that a normal event handler can perform
- Declaring the same reactive variable twice
Key Points
- The dollar colon label makes a statement reactive
- Declarations, blocks, and conditions all use the same label
- Svelte orders statements by their dependencies
- Only variables written directly in the statement count as dependencies
