Reactive blocks let you run code automatically when some data changes. This helps keep your app updated without extra work.
When you want to update something on the screen after a variable changes.
When you need to run a function automatically after data updates.
When you want to log or track changes in your app state.
When you want to fetch new data after a user input changes.
When you want to trigger animations or effects based on state changes.
Syntax
Svelte
$: {
// code here runs when used variables change
}
The $: label marks a reactive block in Svelte.
Code inside runs automatically when any variable it uses changes.
Examples
This logs the value of count every time it changes.
Svelte
$: console.log(count);
This recalculates total whenever price or quantity changes.
Svelte
$: {
total = price * quantity;
}
This shows an alert when count goes above 10.
Svelte
$: {
if (count > 10) {
alert('Count is over 10!');
}
}
Sample Program
This Svelte component shows a button and a count number. When you click the button, count increases by 1. The reactive block logs the new count value automatically every time it changes.
Svelte
<script>
let count = 0;
// Reactive block runs when count changes
$: {
console.log(`Count changed to ${count}`);
}
function increment() {
count += 1;
}
</script>
<button on:click={increment} aria-label="Increase count">Increase Count</button>
<p>Current count: {count}</p>
OutputSuccess
Important Notes
Reactive blocks run after the component updates the DOM.
Use reactive blocks for side effects, not for returning values.
They help keep your code clean by avoiding manual watchers or event listeners.
Summary
Reactive blocks run code automatically when variables change.
Use $: label to create a reactive block in Svelte.
Great for side effects like logging, alerts, or updating related data.
Practice
(1/5)
1. What does a reactive block labeled with $: do in Svelte?
easy
A. It runs the code inside automatically whenever referenced variables change.
B. It defines a new component in Svelte.
C. It creates a CSS style block scoped to the component.
D. It pauses the component rendering until a condition is met.
Solution
Step 1: Understand the role of $: in Svelte
The $: label creates a reactive block that runs code automatically when variables it uses change.
Step 2: Identify the behavior of reactive blocks
Reactive blocks are used for side effects like logging or updating related data, triggered by variable changes.
Final Answer:
It runs the code inside automatically whenever referenced variables change. -> Option A
Quick Check:
Reactive block = auto-run on variable change [OK]
Hint: Reactive blocks run code when variables they use change [OK]
Common Mistakes:
Thinking reactive blocks define components
Confusing reactive blocks with CSS or styling
Believing reactive blocks pause rendering
2. Which of the following is the correct syntax to create a reactive block in Svelte?
easy
A. $: { console.log(count); }
B. reactive: { console.log(count); }
C. @reactive { console.log(count); }
D. #: { console.log(count); }
Solution
Step 1: Recall the syntax for reactive blocks in Svelte
Svelte uses the $: label followed by a block or statement to create reactive code.
Step 2: Match the correct syntax
$: { console.log(count); } uses $: { ... } which is the correct way to write a reactive block.
Final Answer:
$: { console.log(count); } -> Option A
Quick Check:
Reactive block syntax = $: [OK]
Hint: Look for the $: label before the block [OK]
Common Mistakes:
Using 'reactive:' or '@reactive' which are invalid
Confusing with other symbols like '#' or '@'
Missing the $: label entirely
3. Consider this Svelte code:
let count = 0;
$: console.log(`Count is ${count}`);
function increment() {
count += 1;
}
What happens when the increment() function is called?
medium
A. The console logs 'Count is 0' every time.
B. Nothing is logged because reactive blocks don't run on function calls.
C. The console logs 'Count is 1', 'Count is 2', etc., each time increment is called.
D. An error occurs because reactive blocks cannot use template strings.
Solution
Step 1: Understand reactive block behavior with variable changes
The reactive block logs the current value of count whenever count changes.
Step 2: Analyze what happens on calling increment()
Calling increment() increases count by 1, triggering the reactive block to log the updated count.
Final Answer:
The console logs 'Count is 1', 'Count is 2', etc., each time increment is called. -> Option C
Quick Check:
Reactive block logs on variable change = The console logs 'Count is 1', 'Count is 2', etc., each time increment is called. [OK]
Hint: Reactive blocks run when variables they use change [OK]