This Svelte component uses an advanced feature called a store to share and update a count value. The button updates the count, and the display updates automatically. This pattern helps build apps that are easy to maintain and scale.
Svelte
<script>
import { count } from'./store.js';
import { onDestroy } from'svelte';
// Subscribe to the storelet currentCount;
const unsubscribe = count.subscribe(value => {
currentCount = value;
});
function increment() {
count.update(n => n + 1);
}
// Clean up subscription when component is destroyed
onDestroy(() => {
unsubscribe();
});
</script>
<button on:click={increment} aria-label="Increment count">
Count is {currentCount}
</button>
OutputSuccess
Important Notes
Using stores helps keep your app's data consistent across many parts.
Reactive statements update the UI automatically when data changes.
Lifecycle hooks like onMount help run code at the right time.
Summary
Advanced features make apps faster and easier to manage.
They help handle data, user actions, and app state smoothly.
Using these features prepares your app for real-world use.
Practice
(1/5)
1. Which advanced Svelte feature helps automatically update the UI when data changes without manual DOM manipulation?
easy
A. Reactive statements using $:
B. Using bind:this to reference DOM elements
C. Creating custom events with createEventDispatcher
D. Using on:click event handlers
Solution
Step 1: Understand reactive statements
Reactive statements with $: automatically run when their dependencies change, updating the UI.
Step 2: Compare other options
bind:this references DOM nodes, createEventDispatcher creates events, and on:click handles clicks, but none auto-update UI on data change.
Final Answer:
Reactive statements using $: -> Option A
Quick Check:
Auto UI update = Reactive statements [OK]
Hint: Look for features that auto-update UI on data change [OK]
Common Mistakes:
Confusing event handlers with reactive updates
Thinking bind:this updates UI automatically
Assuming custom events trigger UI refresh
2. Which of the following is the correct syntax to declare a reactive variable in Svelte?
easy
A. function doubled() { return count * 2; }
B. let count = 0;
C. const doubled = count * 2;
D. $: doubled = count * 2;
Solution
Step 1: Identify reactive syntax
In Svelte, reactive variables use $: before the statement to auto-update when dependencies change.
Step 2: Check other options
let count = 0; is normal variable declaration, const and function do not create reactive variables.
Final Answer:
$: doubled = count * 2; -> Option D
Quick Check:
Reactive variable = $: syntax [OK]
Hint: Reactive variables start with $: in Svelte [OK]
Common Mistakes:
Using const or function instead of $: for reactivity
Forgetting the $: prefix for reactive statements
Assuming let alone makes a variable reactive
3. What will be the output in the browser console when this Svelte code runs?
medium
A. 2
B. 2 and then 6
C. 6
D. 1
Solution
Step 1: Understand reactive statement timing
Initially, count is 1, so doubled = 2. The console.log runs once during setup, logging 2.
Step 2: Analyze assignment after console.log
Changing count to 3 triggers the reactive statement again, updating doubled to 6 and logging it again.
Final Answer:
2 and then 6 -> Option B
Quick Check:
Console logs twice with doubled=2 then 6 [OK]
Hint: Console.log runs initially and again after count changes [OK]
Common Mistakes:
Expecting console.log to run only once
Confusing doubled value with count value
Thinking reactive statements log automatically multiple times without explicit logging
4. Identify the error in this Svelte code snippet that prevents the reactive statement from updating correctly:
count++}>Increment
{doubled}
medium
A. No error; code works correctly
B. Reactive statement lacks a dependency
C. Reactive statement missing a trailing semicolon
D. Missing semicolon after reactive statement
Solution
Step 1: Check reactive statement syntax
In Svelte, semicolons are optional; the reactive statement $: doubled = count * 2 is valid without a semicolon.
Step 2: Verify reactivity and event handling
The button increments count, which triggers the reactive statement to update doubled and display it correctly.
Final Answer:
No error; code works correctly -> Option A
Quick Check:
Semicolons optional; reactive updates work [OK]
Hint: Svelte allows missing semicolons; check logic not punctuation [OK]
Common Mistakes:
Assuming missing semicolon causes error
Thinking reactive statements need explicit dependencies
Overlooking that Svelte auto-tracks dependencies
5. You want to build a production-ready Svelte app that efficiently updates a list of items when data changes. Which advanced feature should you use to ensure minimal DOM updates and smooth user experience?
hard
A. Manually manipulate DOM elements with document.querySelector
B. Use global variables to store list data
C. Use each blocks with keyed elements
D. Reload the entire page on every data change
Solution
Step 1: Understand efficient list updates in Svelte
Using each blocks with keys lets Svelte track items and update only changed elements, minimizing DOM changes.
Step 2: Evaluate other options
Manual DOM manipulation is error-prone and defeats Svelte's reactivity; global variables don't trigger updates; reloading page is inefficient.
Final Answer:
Use each blocks with keyed elements -> Option C
Quick Check:
Keyed each blocks = efficient updates [OK]
Hint: Key list items in each blocks for smooth updates [OK]
Common Mistakes:
Trying manual DOM changes instead of reactive updates
Using global variables without reactivity
Reloading page wastes resources and breaks UX
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.