Complete the code to auto-subscribe to the store and display its value.
<script> import { writable } from 'svelte/store'; const count = writable(0); </script> <p>Count: {{$ [1] }}</p>
Using $count auto-subscribes to the store and updates the value reactively.
Complete the code to update the store value on button click using auto-subscription.
<script> import { writable } from 'svelte/store'; const count = writable(0); function increment() { count.[1](n => n + 1); } </script> <button on:click={increment}>Increment</button> <p>Count: {$count}</p>
set without the new value.subscribe to change the value.The update method lets you change the store value based on its current value.
Fix the error in the code to correctly auto-subscribe to the store value.
<script> import { writable } from 'svelte/store'; const name = writable('Alice'); </script> <p>Hello, {{$ [1] }}</p>
Inside the template, use $name without extra $ or parentheses. The code already has $ before the blank, so just put name.
Fill both blanks to create a derived store and auto-subscribe to its value.
<script> import { writable, derived } from 'svelte/store'; const a = writable(2); const b = writable(3); const sum = derived([a, b], ([$a, $b]) => $a [1] $b); </script> <p>Sum: {{$ [2] }}</p>
The derived store sum adds $a and $b. Use sum with $ prefix to auto-subscribe.
Fill all three blanks to create a reactive statement that logs the store value whenever it changes.
<script> import { writable } from 'svelte/store'; const score = writable(0); $: console.log([1]); // logs the current score function increase() { score.[2](n => n + 1); } </script> <button on:click={increase}>Increase</button> <p>Score: {{$ [3] }}</p>
The reactive statement uses $score to auto-subscribe and log changes. The update method changes the store value. The template uses $score to display the current value, but since the $ is already in the code, just put score.