A custom store in Svelte is a way to create your own reactive data source with custom logic for updating and subscribing, beyond the built-in writable or readable stores.
Click to reveal answer
beginner
Which function do you use to create a custom store in Svelte?
You create a custom store by writing a function that returns an object with a subscribe method, and optionally set and update methods for writable behavior.
Click to reveal answer
intermediate
Why would you use a custom store instead of a writable store in Svelte?
You use a custom store when you want to add extra logic like validation, side effects, or complex state management that writable stores don't support directly.
Click to reveal answer
beginner
What does the subscribe method in a custom store do?
The subscribe method lets components listen to changes in the store's value and reactively update when the value changes.
Click to reveal answer
intermediate
How can you add a cleanup function in a custom store?
Inside the subscribe method, you can return a function that runs when the last subscriber unsubscribes, useful for cleaning up resources like timers or event listeners.
Click to reveal answer
What must a custom store in Svelte always provide?
AAn update method
BA set method
CA subscribe method
DA reset method
✗ Incorrect
The subscribe method is required for any Svelte store so components can listen to changes.
Which of these is NOT a reason to create a custom store?
ATo automatically style components
BTo share reactive state
CTo add validation logic
DTo handle side effects on state changes
✗ Incorrect
Styling components is unrelated to store logic; stores manage data and reactivity.
How do you notify subscribers about a new value in a custom store?
AUse console.log
BCall setTimeout
CReturn a cleanup function
DCall the subscriber functions with the new value
✗ Incorrect
Subscribers are functions that must be called with the updated value to trigger reactive updates.
What does the cleanup function returned by subscribe do?
ARuns when the last subscriber unsubscribes
BRuns every time the value changes
CRuns before the first subscriber subscribes
DRuns when the store is created
✗ Incorrect
The cleanup function is called when no subscribers remain, to free resources.
Which Svelte store type is easiest to customize with extra logic?
ADerived store
BCustom store
CWritable store
DReadable store
✗ Incorrect
Custom stores let you define any logic you want, unlike built-in stores with fixed behavior.
Explain how to create a custom store in Svelte and why you might want to use one.
Think about how you share reactive data with extra control.
You got /4 concepts.
Describe the role of the subscribe method and the cleanup function in a custom Svelte store.
Focus on how subscriptions start and stop.
You got /4 concepts.
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.