0 day streak
javascript / beginner
Snippet

Reactive Declarations

Reactive declarations automatically re-calculate their value whenever any of the variables they reference change. The $: syntax marks a statement as reactive.

snippet.js
javascript
1
2
3
4
5
6
7
8
<script>
let count = 0;
$: doubled = count * 2;
</script>
<button on:click={() => count++}>
{count} doubled is {doubled}
</button>
svelte
Breakdown
1
let count = 0;
Initializes a standard reactive variable.
2
$: doubled = count * 2;
The $: label tells Svelte to re-run this line whenever 'count' changes.