Reactivity helps the user interface update automatically when data changes. This makes apps feel fast and smooth without extra work.
Why reactivity drives UI updates in Svelte
let count = 0; // When 'count' changes, Svelte updates the UI automatically <p>{count}</p> // To react to changes, use $: label $: doubled = count * 2;
Variables declared with let are reactive in Svelte.
The $: label runs code whenever referenced reactive variables change.
name changes.let name = 'Alice'; <p>Hello {name}!</p>
doubled value updates automatically when count changes.let count = 0; $: doubled = count * 2; <p>{count} doubled is {doubled}</p>
count, and the UI updates instantly.let count = 0; function increment() { count += 1; } <button on:click={increment}>Add 1</button> <p>Count: {count}</p>
This Svelte component shows a button and two numbers. When you click the button, count increases by one. The UI updates automatically to show the new count and its double. This happens because Svelte tracks changes to count and updates the parts of the page that use it.
<script> let count = 0; function increment() { count += 1; } $: doubled = count * 2; </script> <button on:click={increment} aria-label="Add one to count">Add 1</button> <p>Count: {count}</p> <p>Doubled: {doubled}</p>
Reactivity in Svelte means you don't have to write extra code to update the UI.
Use the $: label to create reactive statements that run when data changes.
Always declare reactive variables with let to allow updates.
Reactivity keeps the UI and data in sync automatically.
Svelte updates only the parts of the page that depend on changed data.
This makes apps faster and easier to build.
Practice
Solution
Step 1: Understand Svelte's reactivity system
Svelte automatically tracks variables marked reactive and listens for their changes.Step 2: Connect reactivity to UI updates
When a reactive variable changes, Svelte updates only the parts of the UI that depend on it without a full page reload.Final Answer:
Because Svelte tracks reactive variables and updates the UI when they change -> Option DQuick Check:
Reactivity = automatic UI update [OK]
- Thinking the whole page reloads on variable change
- Believing manual UI update calls are needed
- Assuming timers check for changes
Solution
Step 1: Identify reactive declaration syntax
Svelte usesletto declare reactive variables at the top level. Reassigning them triggers automatic UI updates.Step 2: Check each option
let count = 0;declares a reactive variable.var $count = 0;andconst count = reactive(0);are invalid syntax.$: doubled = count * 2;is a reactive statement for derived values.Final Answer:
let count = 0;-> Option AQuick Check:
Reactive variables =let[OK]
let declares reactive variables [OK]- Thinking
$:is required for basic variables - Using invalid syntax like
reactive()or$var - Confusing reactive statements with variable declarations
let count = 1;
$: doubled = count * 2;
<button on:click={() => count++}>Increment</button>
<p>Doubled: {doubled}</p>What will be displayed after clicking the button twice?
Solution
Step 1: Track variable changes on clicks
Initial count is 1. Each click increments count by 1. After two clicks, count = 3.Step 2: Calculate doubled value reactively
doubled = count * 2 = 3 * 2 = 6. The UI updates to show doubled as 6.Final Answer:
Doubled: 6 -> Option CQuick Check:
count=3, doubled=6 [OK]
- Forgetting count increments twice
- Using old count value for doubled
- Confusing doubled with count
count changes?let count = 0;
function increment() {
count = count + 1;
}
<button on:click={increment}>Add</button>
<p>Count: {count}</p>Solution
Step 1: Check reactive variable usage
In Svelte, normal variables declared withletare reactive by default when reassigned.Step 2: Verify event handler and UI binding
Theon:click={increment}syntax is correct, and{count}will update when count changes.Final Answer:
The UI will update automatically; no error here -> Option AQuick Check:
Reassignment triggers UI update [OK]
- Thinking
$:is needed for all variables - Believing event handler syntax is wrong
- Assuming manual UI refresh is required
Solution
Step 1: Understand reactive statements for derived data
Svelte's$:label allows creating reactive variables that update automatically when dependencies change.Step 2: Apply reactive statement to selection count
Using$: selectedCount = items.filter(i => i.selected).length;ensures selectedCount updates whenever items or their selected property changes.Final Answer:
Use a reactive statement like $: selectedCount = items.filter(i => i.selected).length; -> Option BQuick Check:
Reactive derived data updates automatically [OK]
- Manually updating counts instead of reactive statements
- Using global variables causing stale data
- Updating count only once on mount
Ready to prove it?
Pass the test and this pattern moves unknown
