Context API and stores help share data between components in Svelte without passing props everywhere.
Context API vs stores in Svelte
/* Context API in Svelte */ // In parent component import { setContext } from 'svelte'; setContext('key', value); // In child component import { getContext } from 'svelte'; const value = getContext('key'); /* Stores in Svelte */ import { writable } from 'svelte/store'; const count = writable(0); // In any component import { count } from './store.js'; $count; // reactive value count.set(5); // update value
Context API shares data only between parent and its descendants.
Stores can be imported anywhere and provide reactive shared state.
// Using Context API // Parent.svelte <script> import { setContext } from 'svelte'; setContext('user', { name: 'Anna' }); </script> // Child.svelte <script> import { getContext } from 'svelte'; const user = getContext('user'); </script> <p>User: {user.name}</p>
// Using a writable store // store.js import { writable } from 'svelte/store'; export const count = writable(0); // Counter.svelte <script> import { count } from './store.js'; </script> <button on:click={() => count.update(n => n + 1)}> Count: {$count} </button>
This example shows how Context API passes a color string from parent to child. The store shares a message string that child can update and reactively display.
<!-- App.svelte --> <script> import { setContext } from 'svelte'; import Child from './Child.svelte'; import { writable } from 'svelte/store'; // Using Context API setContext('color', 'blue'); // Using Store export const message = writable('Hello from store!'); </script> <h1>Parent Component</h1> <Child /> <!-- Child.svelte --> <script> import { getContext } from 'svelte'; import { message } from './App.svelte'; const color = getContext('color'); </script> <p style="color: {color}">Context color: {color}</p> <p>Store message: {$message}</p> <button on:click={() => message.set('Updated message!')}>Update Store</button>
Context API is good for passing data down a component tree without props.
Stores are better for global or shared reactive state accessible anywhere.
Use stores when many components need to read and write the same data.
Context API shares data only between parent and children components.
Stores provide reactive global state accessible from any component.
Choose Context API for simple parent-child data sharing, stores for app-wide state.
Practice
Solution
Step 1: Understand Context API scope
Context API shares data only between parent and its children components, not globally.Step 2: Understand stores scope
Stores provide reactive global state accessible from any component in the app.Final Answer:
Context API shares data only between parent and child components, while stores provide global reactive state accessible anywhere. -> Option CQuick Check:
Context API = parent-child, stores = global state [OK]
- Thinking stores only pass data down like props
- Confusing Context API with global state management
- Assuming stores are for styling or lifecycle
Solution
Step 1: Recall writable store methods
Svelte writable stores have a set() method to assign a new value.Step 2: Check other options
store.value is not valid, update() takes a function, change() does not exist.Final Answer:
store.set(newValue); -> Option AQuick Check:
Use set() to assign store value [OK]
- Trying to assign store.value directly
- Using update() with a value instead of a function
- Using non-existent methods like change()
import { setContext, getContext } from 'svelte';
// Parent.svelte
setContext('color', 'blue');
// Child.svelte
const color = getContext('color');
What will
color be inside Child.svelte?Solution
Step 1: Understand setContext and getContext
setContext('color', 'blue') sets a value accessible to child components via getContext('color').Step 2: Check child access
Child.svelte calls getContext('color') and receives 'blue' because it is a child of the parent setting the context.Final Answer:
'blue' -> Option DQuick Check:
Context API passes 'blue' from parent to child [OK]
- Expecting undefined if not wrapped properly
- Confusing context keys or forgetting to setContext
- Assuming context works globally like stores
import { writable } from 'svelte/store';
const count = writable(0);
function increment() {
count = count + 1;
}Solution
Step 1: Identify store usage error
The code tries to do count = count + 1, which reassigns the store variable instead of updating its value.Step 2: Correct way to update store
Use count.update(n => n + 1) or count.set(newValue) to change the store's value reactively.Final Answer:
You cannot reassign the store variable; must use count.update() or count.set(). -> Option BQuick Check:
Update store with update() or set(), not reassignment [OK]
- Trying to assign store variable directly
- Ignoring reactive update methods
- Thinking writable stores can't hold numbers
Solution
Step 1: Identify requirement for global reactive state
The theme color must be shared app-wide and update reactively from any component.Step 2: Compare Context API and stores
Context API only shares data between parent and children, not globally. Stores provide reactive global state accessible anywhere.Final Answer:
Use a writable store for the theme color because stores provide reactive global state accessible anywhere. -> Option AQuick Check:
Global reactive state needs stores, not Context API [OK]
- Choosing Context API for global state
- Passing props everywhere manually
- Using local variables that don't sync
Ready to prove it?
Pass the test and this pattern moves unknown
