A reactive block in Svelte is a special code section that runs automatically whenever the variables it depends on change. It is used to perform side effects or update values reactively.
Click to reveal answer
beginner
How do you write a reactive block in Svelte?
You write a reactive block using the $: label followed by a block of code inside curly braces. For example: $: { console.log(count); } runs whenever count changes.
Click to reveal answer
intermediate
Why use reactive blocks for side effects in Svelte?
Reactive blocks let you run code automatically when data changes, like updating the DOM, logging, or fetching data. This keeps your code simple and avoids manual event handling.
Click to reveal answer
intermediate
Can reactive blocks depend on multiple variables?
Yes, reactive blocks run whenever any variable they use changes. You can use multiple variables inside the block, and Svelte tracks all dependencies automatically.
Click to reveal answer
advanced
What happens if a reactive block updates a variable it depends on?
If a reactive block updates a variable it depends on, it can cause the block to run again, potentially creating an infinite loop. You should avoid such patterns or add conditions to stop looping.
Click to reveal answer
How do you start a reactive block in Svelte?
A$:
Breactive()
CuseEffect()
DonMount()
✗ Incorrect
Reactive blocks in Svelte start with the $: label.
When does a reactive block run?
AWhenever any variable inside it changes
BOnly once when the component loads
CWhen the user clicks a button
DOnly when explicitly called
✗ Incorrect
Reactive blocks run automatically whenever any variable they use changes.
Which of these is a good use for a reactive block?
AWriting HTML markup
BStyling elements with CSS
CDefining component props
DFetching data when a variable changes
✗ Incorrect
Reactive blocks are great for side effects like fetching data when variables change.
What risk should you watch for when updating variables inside a reactive block?
AMemory leaks
BInfinite loops
CSyntax errors
DSlow rendering
✗ Incorrect
Updating a variable inside a reactive block that depends on it can cause infinite loops.
Can reactive blocks access variables declared outside them?
ANo, only inside variables
BOnly if passed as props
CYes, and they track changes automatically
DOnly if declared as constants
✗ Incorrect
Reactive blocks can use any variables in scope and track their changes automatically.
Explain how reactive blocks help manage side effects in Svelte components.
Think about how Svelte tracks variables and runs code when they update.
You got /4 concepts.
Describe a potential problem when a reactive block updates a variable it depends on and how to avoid it.
Consider what happens if the block triggers itself continuously.
You got /4 concepts.
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]