By the end of this lesson, you will be able to:
$:
syntax:information_source: Info Svelte reactivity connects to JavaScript's DOM (Document Object Model). When a user changes values on your website, reactivity makes those changes appear automatically in the DOM - no extra code needed!
Making your Svelte components reactive is simple:
=
): The easiest way to trigger reactivity:bulb: Tip What's a Top-Level Variable? A top-level variable is any variable you create directly inside the
<script>
tag. It's not inside a function or block - it sits at the "top level" of your script!
Let's see how reactivity works with a simple example:
app.svelte
<script>
let number = 0;
function updateNum() \{
number = 25;
\}
</script>
<button on:click=\{updateNum\}>Update</button>
<p>\{number\}</p>
Output BEFORE the button is clicked:
Output AFTER the button is clicked:
In our example:
number
is our top-level variable (it's directly in the <script>
tag)updateNum()
changes number
to 25:memo: Key Insight Svelte watches for assignments to top-level variables. When it sees
number = 25
, it knows to update everything that usesnumber
in your component!
Sometimes you need values that depend on other values. Svelte has a special syntax for this!
$:
Syntax$:
(dollar sign + colon) before your declarationLet's see it in action:
app.svelte
<script>
let number = 0;
$: add_with_two = number + 2;
function clickButton() \{
number = number + 1;
\}
</script>
<button on:click=\{clickButton\}>
Clicked \{number\} time
</button>
<p>\{number\} add with 2 is \{add_with_two\}</p>
Output BEFORE the button is clicked:
Output AFTER the button is clicked 1 time:
Output AFTER the button is clicked 2 times:
Looking at our example:
add_with_two
depends on number
number
changes, add_with_two
recalculates:bulb: Tip Try This! Without
$:
, theadd_with_two
value would stay at 2 forever, even whennumber
changes. The reactive declaration makes it update automatically!
Reactive declarations aren't just for values - you can run any code reactively!
$:
with if statements, console.log, or any JavaScriptHere's how to use reactive statements with conditions:
app.svelte
<script>
let number = 0;
$: if (number > 3) \{
alert(`You clicked more than 3 times!`);
alert(`You have clicked ` + number + ` times`);
\}
function clickButton() \{
number = number + 1;
\}
</script>
<button on:click=\{clickButton\}>
Clicked \{number\} time
</button>
Output BEFORE the button is clicked:
Output AFTER the button is clicked 4 times:
In this example:
$:
makes the if statement reactivenumber
changes, Svelte checks the conditionnumber
goes above 3, the alerts trigger automatically:memo: Important! Without
$:
, the if statement would only run once when the component loads. The reactive declaration makes it check the condition every timenumber
changes!
Need to run several things when a value changes? You can group multiple statements together!
Use curly braces {}
to run multiple statements reactively:
app.svelte
<script>
let number = 0;
$: \{
console.log("---");
console.log("Number of times clicked:", number);
\}
function clickButton() \{
number = number + 1;
\}
</script>
<button on:click=\{clickButton\}>
Click me
</button>
Output BEFORE the button is clicked:
Output AFTER the button is clicked 3 times:
This pattern is useful for:
:memo: Console Tip Check your browser's Developer Console (F12) to see the console.log outputs. It's a great way to debug your reactive code!
Let's recap what we learned about Svelte Reactivity:
<script>
tag are reactive by default=
to trigger reactivity$:
to create values that depend on other values$:
to run code when values change{}
:bulb: Tip Remember This! The magic of Svelte is that you write normal JavaScript, and Svelte makes it reactive. No special methods or complex state management needed!
Try these exercises to master Svelte reactivity:
Create a component with:
Build a component that:
$:
Create a component with:
Build a mini shopping cart that:
Ready to build amazing reactive components? Watch the video above and start coding! :rocket: