Reactive declarations let your code automatically update when data changes. It helps keep your app in sync without extra work.
Reactive declarations (let) in Svelte
let derived = expression;
$: derived = expression;The $: label marks a reactive declaration.
Whenever variables inside the expression change, the reactive declaration runs again.
doubled that updates when count changes.let count = 0; $: doubled = count * 2;
greeting updates automatically when name changes.let name = 'Alice'; $: greeting = `Hello, ${name}!`;
sum recalculates whenever a or b changes.let a = 5; let b = 10; $: sum = a + b;
This Svelte component shows a button with a count. When you click it, count increases. The doubled value updates automatically and shows below.
It uses a reactive declaration $: doubled = count * 2; to keep doubled in sync.
<script> let count = 1; $: doubled = count * 2; function increment() { count += 1; } </script> <button on:click={increment} aria-label="Increase count">Count: {count}</button> <p>Doubled value: {doubled}</p>
Reactive declarations run after the component initializes and whenever dependencies change.
You can use multiple reactive declarations in one component.
Use let to declare variables that will change; reactive declarations update them automatically.
Reactive declarations automatically update values when dependencies change.
Use $: before a statement to make it reactive.
This helps keep your UI and data in sync with less code.
Practice
$: symbol do in a Svelte component?Solution
Step 1: Understand the role of
The$:in Svelte$:prefix is used to declare reactive statements that automatically update when their dependencies change.Step 2: Compare with other options
Options A, B, and D describe unrelated concepts: imports, constants, and comments, which are not related to$:.Final Answer:
It marks a statement as reactive, updating when dependencies change. -> Option DQuick Check:
Reactive declarations use$:= C [OK]
$: means reactive update in Svelte [OK]- Thinking
$:declares constants - Confusing
$:with import syntax - Assuming
$:comments code
sum that adds a and b in Svelte?Solution
Step 1: Identify reactive declaration syntax
In Svelte, reactive declarations use$:before the statement to update automatically when dependencies change.Step 2: Check each option
let sum = a + b;is a normal variable assignment without reactivity.sum := a + b;uses invalid syntax.const $sum = a + b;incorrectly usesconstand a dollar sign in the variable name.Final Answer:
$: sum = a + b; -> Option CQuick Check:
Reactive declaration =$: sum = a + b;[OK]
$: followed by assignment [OK]- Omitting
$:for reactive variables - Using invalid assignment operators like ':='
- Trying to declare reactive constants with
const
sum after a changes to 5?let a = 2; let b = 3; $: sum = a + b; // Later in code a = 5;
Solution
Step 1: Understand reactive declaration behavior
The reactive declaration$: sum = a + b;recalculatessumwheneveraorbchanges.Step 2: Calculate new value after
Initially,achangesa = 2andb = 3, sosum = 5. Whenachanges to 5,sumupdates to5 + 3 = 8.Final Answer:
8 -> Option AQuick Check:
sum updates reactively = 8 [OK]
- Assuming sum stays old value after a changes
- Confusing sum with just a or b
- Expecting syntax error due to reassignment
let x = 1; $: y = x * 2 $: z = y + 3
Solution
Step 1: Check syntax and reactive dependencies
The code uses reactive declarations correctly. Semicolons are optional in JavaScript and Svelte allows reactive variables to depend on other reactive variables.Step 2: Validate variable declarations
Variables declared withletcan be used reactively.constis not required inside reactive declarations.Final Answer:
The code is correct and will work as expected. -> Option AQuick Check:
Reactive chaining allowed = D [OK]
- Thinking semicolons are mandatory
- Believing reactive vars can't depend on others
- Forcing
constinstead oflet
status that shows "High" if score is above 80, "Medium" if between 50 and 80, and "Low" otherwise. Which reactive declaration correctly implements this?Solution
Step 1: Understand conditional expressions in reactive declarations
Svelte reactive declarations use JavaScript syntax. Nested ternary operators correctly express multiple conditions.Step 2: Evaluate each option's correctness
$: status = score > 80 ? 'High' : score >= 50 ? 'Medium' : 'Low';uses valid nested ternary syntax.let status = if (score > 80) 'High' else if (score >= 50) 'Medium' else 'Low';uses invalid syntax (noifinside assignment).$: if score > 80 then status = 'High' else status = 'Low';uses invalid syntax (nothenkeyword in JS).$: status = score > 80 && 'High' || score >= 50 && 'Medium' || 'Low';uses logical operators that may cause incorrect evaluation.Final Answer:
$: status = score > 80 ? 'High' : score >= 50 ? 'Medium' : 'Low'; -> Option BQuick Check:
Nested ternary for reactive status = A [OK]
- Using invalid if-else syntax inside reactive declarations
- Confusing logical operators with ternary conditions
- Omitting
$:for reactive updates
Ready to prove it?
Pass the test and this pattern moves unknown
