0
0
Svelteframework~5 mins

Custom store logic in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom store in Svelte?
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
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
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
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
Which Svelte store type is easiest to customize with extra logic?
ADerived store
BCustom store
CWritable store
DReadable store
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.