Derived stores let you create new values based on one or more existing stores. They update automatically when the original stores change.
Derived stores in Svelte
import { derived } from 'svelte/store'; const derivedStore = derived( [store1, store2], ([$store1, $store2]) => $store1 + $store2, initialValue );
You pass one or more stores as the first argument (array or single store).
The callback receives the current values of those stores and returns the new value.
double automatically doubles the value of count.import { writable, derived } from 'svelte/store'; const count = writable(2); const double = derived(count, $count => $count * 2);
sum adds values from two stores a and b.import { writable, derived } from 'svelte/store'; const a = writable(3); const b = writable(4); const sum = derived([a, b], ([$a, $b]) => $a + $b);
greeting creates a greeting message from name.import { writable, derived } from 'svelte/store'; const name = writable('Alice'); const greeting = derived(name, $name => `Hello, ${$name}!`);
This example shows a writable store celsius and a derived store fahrenheit that converts the temperature. When celsius changes, fahrenheit updates automatically.
import { writable, derived } from 'svelte/store'; // Create a writable store for temperature in Celsius const celsius = writable(25); // Create a derived store to convert Celsius to Fahrenheit const fahrenheit = derived(celsius, $celsius => ($celsius * 9 / 5) + 32); // Subscribe to both stores and log their values celsius.subscribe(value => console.log(`Celsius: ${value}°C`)); fahrenheit.subscribe(value => console.log(`Fahrenheit: ${value}°F`)); // Change Celsius value to see automatic update celsius.set(30);
Derived stores are read-only by default. You cannot set their value directly.
If you only use one store, you can pass it directly instead of an array.
Use derived stores to keep your data logic clean and avoid repeating calculations.
Derived stores create new values based on other stores.
They update automatically when the original stores change.
Use them to combine, transform, or calculate store data easily.
Practice
derived store in Svelte?Solution
Step 1: Understand what derived stores do
Derived stores create new values that depend on one or more other stores.Step 2: Identify the main purpose
They automatically update when the original stores change, reflecting combined or transformed data.Final Answer:
To create a new store that automatically updates based on other stores -> Option AQuick Check:
Derived stores update automatically = B [OK]
- Thinking derived stores hold static values
- Confusing derived with writable stores
- Believing derived stores handle events
a and b?Solution
Step 1: Recall derived store syntax
Derived stores take an array of stores and a callback with destructured values.Step 2: Match correct syntax
The callback receives an array destructured as[$a, $b]to access current values.Final Answer:
const c = derived([a, b], ([$a, $b]) => $a + $b); -> Option BQuick Check:
Array + destructured callback = A [OK]
- Passing stores as separate arguments instead of array
- Not destructuring the values in callback
- Using wrong parameter order in callback
a.set(3) is called?import { writable, derived } from 'svelte/store';
const a = writable(1);
const b = writable(2);
const sum = derived([a, b], ([$a, $b]) => $a + $b);
sum.subscribe(value => console.log(value));
a.set(3);Solution
Step 1: Understand initial subscription output
When subscribing, the current derived value is logged immediately: 1 + 2 = 3.Step 2: Update store
Afterato 3 and check derived valuea.set(3), derived recalculates: 3 + 2 = 5, which is logged.Final Answer:
3, 5 -> Option DQuick Check:
Initial + updated sum = 3, 5 [OK]
- Expecting multiple logs before subscription
- Confusing store values with derived values
- Missing initial subscription log
import { writable, derived } from 'svelte/store';
const count = writable(0);
const double = derived(count, value => value * 2, 0);
Solution
Step 1: Check derived store syntax for single store
Derived stores can take a single store and a callback with the value directly.Step 2: Understand the third argument
The third argument is the initial value before the first store value arrives, which is valid.Final Answer:
There is no error; this code is correct -> Option AQuick Check:
Single store + initial value allowed = A [OK]
- Thinking callback must destructure array always
- Believing derived needs array even for one store
- Assuming third argument is invalid
firstName and lastName. You want to create a derived store fullName that updates automatically and trims whitespace from both names before combining. Which code correctly implements this?Solution
Step 1: Use array of stores and destructure values
Derived stores with multiple sources require an array and destructuring in the callback.Step 2: Trim both values before combining
Usetrim()on both$firstNameand$lastNamebefore concatenation.Final Answer:
const fullName = derived([firstName, lastName], ([$firstName, $lastName]) => `${$firstName.trim()} ${$lastName.trim()}`); -> Option CQuick Check:
Array + destructure + trim both = D [OK]
- Not trimming both names
- Passing stores separately, not as array
- Not destructuring values in callback
Ready to prove it?
Pass the test and this pattern moves unknown
