📖 Scenario: You are building a simple Svelte app where multiple components need to share and update a counter value. Instead of passing props down many levels, you will use Svelte's context with stores to share the counter state easily.
🎯 Goal: Create a Svelte app with a store holding a counter value. Provide this store via context in a parent component. Then, in a child component, access the store from context and display and update the counter.
📋 What You'll Learn
Create a writable store called counter with initial value 0
Set the counter store in context using setContext with key 'counter'
In a child component, get the counter store from context using getContext
Display the current counter value and add a button to increment it
💡 Why This Matters
🌍 Real World
Sharing state like counters, user info, or settings across many components without prop drilling.
💼 Career
Understanding context and stores is key for building scalable Svelte apps and collaborating on frontend projects.
Progress0 / 4 steps
1
Create the counter store
In a new Svelte component, import writable from 'svelte/store' and create a writable store called counter with initial value 0.
Svelte
Hint
Use writable(0) to create a store starting at zero.
2
Set the counter store in context
In the parent Svelte component, import setContext from 'svelte' and the counter store. Use setContext with key 'counter' to provide the counter store to child components.
Svelte
Hint
Use setContext('counter', counter) to share the store.
3
Get the counter store from context in child
In a child Svelte component, import getContext from 'svelte'. Use getContext('counter') to get the counter store from context and assign it to a variable called counter.
Svelte
Hint
Use const counter = getContext('counter') to access the store.
4
Display and update the counter in child component
In the child component, use the $counter store value to display the current count inside a <p> tag. Add a button with text Increment that increases the counter by 1 when clicked using counter.update(n => n + 1).
Svelte
Hint
Use {$counter} to show the current value and on:click to handle button clicks.
Practice
(1/5)
1. In Svelte, what is the main purpose of using setContext and getContext with stores?
easy
A. To create a new store that is only used inside one component
B. To handle HTTP requests inside components
C. To style components globally using CSS variables
D. To share reactive data easily between components without passing props through many layers
Solution
Step 1: Understand the role of setContext and getContext
These functions allow sharing data between components without manually passing props down the tree.
Step 2: Recognize the benefit of using stores with context
Stores provide reactive data, and context lets multiple components access the same store easily.
Final Answer:
To share reactive data easily between components without passing props through many layers -> Option D
Quick Check:
Context with stores = share reactive data [OK]
Hint: Context shares stores to avoid prop drilling [OK]
Common Mistakes:
Thinking context creates new stores instead of sharing
Confusing context with styling or HTTP handling
Assuming context replaces all props
2. Which of the following is the correct way to provide a store via context in a Svelte parent component?
easy
A. import { getContext } from 'svelte'; getContext('key', store);
B. import { setContext } from 'svelte'; setContext('key', store);
C. import { setContext } from 'svelte'; setContext(store);
D. import { createContext } from 'svelte'; createContext('key', store);
Solution
Step 1: Recall the correct import and usage for setting context
Use setContext from 'svelte' with a key and a value (store).
Step 2: Identify the correct syntax
The correct call is setContext('key', store); to provide the store under a key.
Final Answer:
import { setContext } from 'svelte'; setContext('key', store); -> Option B
Quick Check:
setContext with key and store = import { setContext } from 'svelte'; setContext('key', store); [OK]
Hint: Use setContext with key and store in parent [OK]
Common Mistakes:
Using getContext instead of setContext to provide
Omitting the key argument in setContext
Using non-existent createContext function
3. Given the following Svelte code, what will be logged to the console?
/* Parent.svelte */
medium
A. 5
B. undefined
C. Error: countStore not found
D. 0
Solution
Step 1: Understand the store creation and context setting
The parent creates a writable store with initial value 5 and sets it in context under 'countStore'.
Step 2: Child retrieves the store and subscribes
The child gets the store from context and subscribes to log its current value, which is 5.
Final Answer:
5 -> Option A
Quick Check:
Store initial value logged = 5 [OK]
Hint: Store initial value logs on subscribe [OK]
Common Mistakes:
Expecting undefined if context key is wrong
Confusing setContext and getContext usage
Assuming subscription logs nothing initially
4. What is the error in this Svelte child component code that tries to get a store from context?
medium
A. Cannot subscribe to context value
B. Using getContext instead of setContext
C. Missing key argument in getContext call
D. Import statement is incorrect
Solution
Step 1: Check getContext usage
getContext requires a key argument to retrieve the correct context value.
Step 2: Identify the missing key
The code calls getContext() without any argument, causing an error.
Final Answer:
Missing key argument in getContext call -> Option C
Quick Check:
getContext needs key argument [OK]
Hint: Always pass key to getContext [OK]
Common Mistakes:
Calling getContext without key
Confusing getContext with setContext
Assuming context value is not subscribable
5. You want to share a writable store with a default value of 0 across multiple nested components using context. Which approach correctly sets and uses this store so that updates in any child reflect everywhere?
hard
A. In parent: setContext('count', writable(0)); In children: const count = getContext('count'); Then use count.update() or count.set()
B. In parent: const count = writable(0); In children: import the store directly and use it
C. In parent: setContext('count', 0); In children: const count = getContext('count'); Then update count directly
D. In parent: setContext('count', writable(0)); In children: const count = getContext('count'); But children cannot update the store
Solution
Step 1: Provide a writable store via context in the parent
Use setContext('count', writable(0)); to share a reactive store.
Step 2: Retrieve the store in children and update it
Children get the store with getContext('count'); and can call update() or set() to change the value, reflecting everywhere.
Final Answer:
In parent: setContext('count', writable(0)); In children: const count = getContext('count'); Then use count.update() or count.set() -> Option A
Quick Check:
Writable store in context updates everywhere [OK]
Hint: Set writable store in context, children get and update it [OK]
Common Mistakes:
Passing raw value instead of store in context
Importing store directly instead of using context
Assuming children cannot update shared store
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.