Simple Counter with Reactive Declarations in Svelte
📖 Scenario: You are building a simple counter app in Svelte. The app shows a number that can be increased by clicking a button. You want to display the double of the current count reactively, so it updates automatically when the count changes.
🎯 Goal: Create a Svelte component that has a count variable, a reactive declaration for double the count, and a button to increase the count. The double value should update automatically when the count changes.
📋 What You'll Learn
Create a
let variable called count initialized to 0.Add a reactive declaration
$: double = count * 2 to calculate double the count.Add a button that increases
count by 1 when clicked.Display both
count and double values in the component.💡 Why This Matters
🌍 Real World
Reactive declarations help build interactive user interfaces that update automatically when data changes, like counters, live search, or dynamic forms.
💼 Career
Understanding reactive declarations is essential for Svelte developers to write clean, efficient, and responsive UI code.
Progress0 / 4 steps