📖 Scenario: You are building a simple Svelte app that shares a user's name across components. You want to learn how to share data using both the Context API and Svelte stores.
🎯 Goal: Build a Svelte app that sets a user's name in a parent component and shares it with a child component using Context API and a store. You will create the data, configure the context and store, use them in child components, and complete the app structure.
📋 What You'll Learn
Create a writable store called userName with initial value "Alice"
Create a context key called userKey with value Symbol()
Set the context with setContext(userKey, userName) in the parent component
Use getContext(userKey) in the child component to access the store
Subscribe to the store in the child component to display the user's name
Use the store directly in another child component to display the user's name
💡 Why This Matters
🌍 Real World
Sharing data between components is common in apps. Context API helps pass data deeply without prop drilling. Stores provide reactive global state. Knowing both helps build clean, maintainable Svelte apps.
💼 Career
Understanding Svelte's Context API and stores is important for frontend developers working with Svelte. It helps manage state efficiently and build scalable component architectures.
Progress0 / 4 steps
1
Create the userName store
In a file called stores.js, import writable from svelte/store and create a writable store called userName with the initial value "Alice".
Svelte
Hint
Use writable to create a store that holds the user's name.
2
Create the context key and set context in parent
In Parent.svelte, import setContext from svelte and userName from ./stores.js. Create a constant userKey with Symbol(). Use setContext(userKey, userName) inside the script to provide the store via context.
Svelte
Hint
Use Symbol() to create a unique key and setContext to share the store.
3
Use getContext to access store in Child component
In Child.svelte, import getContext from svelte. Use const userKey = Symbol() and then get the store with const userName = getContext(userKey). Subscribe to userName store and display the current value inside a <p> tag.
Svelte
Hint
Use getContext with the same key to access the store and subscribe to it.
4
Use the store directly in another Child component
In ChildStore.svelte, import userName from ./stores.js. Use the $userName auto-subscription syntax to display the user's name inside a <p> tag.
Svelte
Hint
Use the $ prefix to auto-subscribe to the store in the template.
Practice
(1/5)
1. Which statement best describes the main difference between Svelte's Context API and stores?
easy
A. Context API is used for styling components, stores manage component lifecycle.
B. Stores are only for passing data from parent to child, Context API is for global state.
C. Context API shares data only between parent and child components, while stores provide global reactive state accessible anywhere.
D. Stores and Context API are identical in functionality and usage.
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 C
Quick Check:
Context API = parent-child, stores = global state [OK]
Hint: Context API is local parent-child, stores are global [OK]
Common Mistakes:
Thinking stores only pass data down like props
Confusing Context API with global state management
Assuming stores are for styling or lifecycle
2. Which of the following is the correct way to set a value in a Svelte writable store?
easy
A. store.set(newValue);
B. store.value = newValue;
C. store.update(newValue);
D. store.change(newValue);
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 A
Quick Check:
Use set() to assign store value [OK]
Hint: Use store.set(value) to update writable stores [OK]
Common Mistakes:
Trying to assign store.value directly
Using update() with a value instead of a function
Using non-existent methods like change()
3. Given this Svelte code using Context API:
import { setContext, getContext } from 'svelte';
// Parent.svelte
setContext('color', 'blue');
// Child.svelte
const color = getContext('color');
What will color be inside Child.svelte?
medium
A. Throws an error
B. undefined
C. null
D. 'blue'
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 D
Quick Check:
Context API passes 'blue' from parent to child [OK]
Hint: getContext returns parent's setContext value [OK]
Common Mistakes:
Expecting undefined if not wrapped properly
Confusing context keys or forgetting to setContext
Assuming context works globally like stores
4. What is wrong with this Svelte store usage?
import { writable } from 'svelte/store';
const count = writable(0);
function increment() {
count = count + 1;
}
medium
A. writable stores cannot hold numbers.
B. You cannot reassign the store variable; must use count.update() or count.set().
C. The import statement is incorrect.
D. The function increment should be async.
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 B
Quick Check:
Update store with update() or set(), not reassignment [OK]
Hint: Use update() or set(), never reassign store variable [OK]
Common Mistakes:
Trying to assign store variable directly
Ignoring reactive update methods
Thinking writable stores can't hold numbers
5. You want to share a theme color across your entire Svelte app and reactively update it from any component. Which approach is best and why?
hard
A. Use a writable store for the theme color because stores provide reactive global state accessible anywhere.
B. Use Context API because it automatically updates all components globally.
C. Pass the theme color as a prop to every component manually.
D. Use a local variable in each component to store the theme color.
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 A
Quick Check:
Global reactive state needs stores, not Context API [OK]
Hint: Global reactive state? Use stores, not Context API [OK]
Common Mistakes:
Choosing Context API for global state
Passing props everywhere manually
Using local variables that don't sync
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.