0
0
Svelteframework~10 mins

Derived stores in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Derived stores
Create base stores
Define derived store with function
Subscribe to base stores
Calculate derived value
Update derived store value
Notify subscribers of derived store
Use derived store in UI
Derived stores listen to base stores, compute new values, and update automatically when base stores change.
Execution Sample
Svelte
import { writable, derived } from 'svelte/store';

const count = writable(1);
const double = derived(count, $count => $count * 2);

double.subscribe(value => console.log(value));
This code creates a writable store 'count' and a derived store 'double' that doubles 'count'. It logs the doubled value whenever 'count' changes.
Execution Table
StepActionBase Store Value (count)Derived Store Value (double)Subscribers Notified
1Initialize count with 11undefinedNo
2Create derived store double from count12No
3Subscribe to double store12Yes (logs 2)
4Update count to 332No
5Derived store recalculates double = 3 * 236Yes (logs 6)
6Update count to 556No
7Derived store recalculates double = 5 * 2510Yes (logs 10)
8No more updates510No
💡 No more updates to base store; derived store value remains stable.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
count11355
doubleundefined261010
Key Moments - 3 Insights
Why does the derived store value update only after the base store changes?
Because derived stores subscribe to base stores and recalculate only when base store values change, as shown between steps 4 and 5 in the execution_table.
Why is the derived store value 'undefined' before creation?
Before the derived store is created, it has no value. At step 1, count is initialized but double is not yet calculated, so it is undefined.
Why do subscribers get notified only after derived store recalculates?
Subscribers are notified only when the derived store value changes, which happens after recalculation triggered by base store updates, as seen in steps 3, 5, and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the derived store value 'double'?
A6
B3
C2
Dundefined
💡 Hint
Check the 'Derived Store Value (double)' column at step 5.
At which step does the subscriber first get notified and log a value?
AStep 1
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Subscribers Notified' column for when logging happens.
If the base store 'count' was updated to 10 at step 6 instead of 5, what would be the derived store value at step 7?
A5
B10
C20
D15
💡 Hint
Derived store doubles the base store value as shown in the calculation at step 7.
Concept Snapshot
Derived stores in Svelte listen to one or more base stores.
They compute new values automatically when base stores change.
Use 'derived(baseStore, callback)' to create one.
Subscribers to derived stores get updated values.
Derived stores help keep related data in sync easily.
Full Transcript
Derived stores in Svelte are special stores that depend on other stores. When you create a derived store, you give it one or more base stores and a function that calculates a new value from those base stores. Whenever a base store changes, the derived store recalculates its value and notifies its subscribers. For example, if you have a writable store 'count' and a derived store 'double' that doubles 'count', then changing 'count' automatically updates 'double'. Subscribers to 'double' get the new doubled value. This helps keep your app data consistent without manual updates.