📖 Scenario: You are building a simple Svelte app that tracks a user's score in a game. You want to create a custom store to manage the score with special logic.
🎯 Goal: Create a custom Svelte store called scoreStore that holds a numeric score starting at 0. Add a method increment to increase the score by 1. Use this store in a Svelte component to display the score and a button to increase it.
📋 What You'll Learn
Create a custom store named scoreStore using Svelte's writable function.
Initialize the store value to 0.
Add a method increment inside the store to add 1 to the current score.
Use the store in a Svelte component to display the current score.
Add a button that calls scoreStore.increment() when clicked to increase the score.
💡 Why This Matters
🌍 Real World
Custom stores let you manage complex state logic in Svelte apps, like game scores, user preferences, or form data.
💼 Career
Understanding custom stores is key for Svelte developers to build maintainable and interactive web applications.
Progress0 / 4 steps
1
Create the initial store
Import writable from 'svelte/store' and create a custom store called scoreStore with an initial value of 0.
Svelte
Hint
Use writable(0) to create a store starting at zero.
2
Add increment method to the store
Inside the createScoreStore function, add a method called increment that uses update to increase the score by 1.
Svelte
Hint
Use increment: () => update(n => n + 1) to add the method.
3
Use the store in a Svelte component
In a Svelte component, import scoreStore and use the $scoreStore syntax to display the current score inside a <p> tag.
Svelte
Hint
Use {$scoreStore} inside the markup to show the current score.
4
Add button to increment the score
Add a <button> element that calls scoreStore.increment() when clicked to increase the score.
Svelte
Hint
Use <button on:click={() => scoreStore.increment()}> to call the increment method.
Practice
(1/5)
1. What is the main purpose of creating a custom store in Svelte?
easy
A. To disable reactivity in the app
B. To add custom methods and control how data updates and shares
C. To make the store work only on the server side
D. To replace the built-in writable store completely
Solution
Step 1: Understand what a custom store does
A custom store extends the basic writable store by adding your own methods to control data updates and sharing.
Step 2: Compare options with this understanding
Only "To add custom methods and control how data updates and shares" correctly describes this purpose. The other options misunderstand the role of custom stores: disabling reactivity, replacing the built-in writable store completely, or server-side only.
Final Answer:
To add custom methods and control how data updates and shares -> Option B
Quick Check:
Custom store purpose = Add custom methods [OK]
Hint: Custom stores add your own methods to writable stores [OK]
The callback uses set(n + 1) but n is not defined anywhere in scope.
Step 2: Understand correct usage
To increment the value, update should be used or the current value tracked. Using set with undefined n causes an error.
Final Answer:
Using set(n + 1) without defining n -> Option C
Quick Check:
Undefined variable in set() causes error [OK]
Hint: Use update() or track value before set() [OK]
Common Mistakes:
Using set() with undefined variable
Forgetting to clear intervals
Confusing set and update methods
5. You want to create a custom store that holds a list of items and provides a method to add unique items only (no duplicates). Which implementation correctly achieves this?
hard
A. import { writable } from 'svelte/store'; function createList() { const { subscribe, update } = writable([]); return { subscribe, add: (item) => update(list => [...list, item]) }; }
B. import { writable } from 'svelte/store'; function createList() { const { subscribe, set } = writable([]); return { subscribe, add: (item) => set(list => list.includes(item) ? list : [...list, item]) }; }
C. import { readable } from 'svelte/store'; function createList() { const { subscribe } = readable([]); return { subscribe, add: (item) => {} }; }
D. import { writable } from 'svelte/store'; function createList() { const { subscribe, update } = writable([]); return { subscribe, add: (item) => update(list => list.includes(item) ? list : [...list, item]) }; }
Solution
Step 1: Check method to add unique items
import { writable } from 'svelte/store'; function createList() { const { subscribe, update } = writable([]); return { subscribe, add: (item) => update(list => list.includes(item) ? list : [...list, item]) }; } uses update and checks if the item exists before adding, preventing duplicates.
Step 2: Verify other options
import { writable } from 'svelte/store'; function createList() { const { subscribe, set } = writable([]); return { subscribe, add: (item) => set(list => list.includes(item) ? list : [...list, item]) }; } incorrectly uses set with a function argument, which is invalid. import { readable } from 'svelte/store'; function createList() { const { subscribe } = readable([]); return { subscribe, add: (item) => {} }; } uses readable which is read-only. import { writable } from 'svelte/store'; function createList() { const { subscribe, update } = writable([]); return { subscribe, add: (item) => update(list => [...list, item]) }; } adds items without checking duplicates.
Final Answer:
Custom store with update() and duplicate check -> Option D
Quick Check:
Use update() and check includes() for uniqueness [OK]
Hint: Use update() with includes() check to avoid duplicates [OK]
Common Mistakes:
Using set() with function argument
Using readable store when write access needed
Not checking for duplicates before adding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.