Context and stores help share data in Svelte apps without passing props everywhere. Choosing between them makes your app simpler and easier to manage.
Context vs stores decision in Svelte
Introduction
Syntax
Svelte
/* Context API */ import { setContext, getContext } from 'svelte'; // In parent component setContext('key', value); // In child component const value = getContext('key'); /* Store API */ import { writable } from 'svelte/store'; const count = writable(0); // In any component count.subscribe(value => { /* react to value */ }); count.set(5);
Context is for passing data down the component tree without props.
Stores hold reactive data accessible anywhere by importing them.
Examples
Svelte
// Using context // Parent.svelte <script> import { setContext } from 'svelte'; setContext('user', { name: 'Anna' }); </script> // Child.svelte <script> import { getContext } from 'svelte'; const user = getContext('user'); console.log(user.name); </script>
Svelte
// Using store // store.js import { writable } from 'svelte/store'; export const count = writable(0); // AnyComponent.svelte <script> import { count } from './store.js'; count.subscribe(value => console.log(value)); count.set(10); </script>
Sample Program
This example shows how context passes the user object from Parent to Child without props.
Svelte
<!-- Parent.svelte --> <script> import { setContext } from 'svelte'; import Child from './Child.svelte'; const user = { name: 'Sam' }; setContext('user', user); </script> <h1>Parent Component</h1> <Child /> <!-- Child.svelte --> <script> import { getContext } from 'svelte'; const user = getContext('user'); </script> <p>Hello, {user.name}!</p>
Important Notes
Use context when data is only needed by child components in a specific part of the tree.
Use stores for app-wide reactive data shared by many components.
Stores can be updated and subscribed to anywhere by importing them.
Summary
Context shares data down the component tree without props.
Stores hold reactive data accessible globally by importing.
Choose context for local sharing, stores for global reactive data.
Practice
1. In Svelte, when should you use
context instead of stores?easy
Solution
Step 1: Understand context usage
Context is designed to share data locally down the component tree without passing props explicitly.Step 2: Compare with stores
Stores are for global reactive data accessible anywhere by importing, not limited to parent-child.Final Answer:
To share data only between a parent and its child components locally -> Option BQuick Check:
Context = local sharing [OK]
Hint: Context is for local, stores for global reactive data [OK]
Common Mistakes:
- Using stores for local-only data sharing
- Confusing context with global state management
- Thinking context is reactive globally
- Using context for styling or async tasks
2. Which of the following is the correct way to set a context value in a Svelte component?
easy
Solution
Step 1: Recall Svelte context API
Svelte usessetContextto provide a value to child components.Step 2: Check import and usage
The correct import is from 'svelte' and the function issetContextwith key and value.Final Answer:
import { setContext } from 'svelte'; setContext('key', value); -> Option CQuick Check:
setContext = provide context [OK]
Hint: Use setContext to provide data down the tree [OK]
Common Mistakes:
- Using createContext or useContext which are React terms
- Trying to set stores with setContext
- Wrong import paths or function names
- Confusing setContext with reactive stores
3. Given this Svelte code, what will be logged to the console?
import { writable } from 'svelte/store';
const count = writable(0);
count.subscribe(value => console.log(value));
count.set(5);
count.update(n => n + 1);
medium
Solution
Step 1: Understand initial subscription call
Subscribing to a writable store immediately calls the callback with the current value, which is 0.Step 2: Trace set and update calls
Thencount.set(5)triggers callback with 5, andcount.update(n => n + 1)triggers callback with 6.Final Answer:
[0, 5, 6] -> Option AQuick Check:
Subscription calls on current and changes [OK]
Hint: Subscription logs current value immediately [OK]
Common Mistakes:
- Ignoring initial subscription call
- Mixing order of set and update
- Assuming update starts from 0 after set
- Confusing subscribe behavior with events
4. What is wrong with this Svelte code snippet that tries to share data using context?
import { getContext } from 'svelte';
const value = getContext('user');
console.log(value);medium
Solution
Step 1: Check context usage location
getContextmust be called inside a component's script during initialization, not in a module scope.Step 2: Understand consequence
Calling it outside component setup returns undefined because no context is set yet.Final Answer:
It tries to get context outside a component setup, causing undefined -> Option AQuick Check:
getContext only works inside component setup [OK]
Hint: Use getContext only inside component scripts [OK]
Common Mistakes:
- Calling getContext in module scope
- Confusing getContext with setContext usage
- Assuming string keys must be objects
- Expecting getContext to have default value argument
5. You want to share a reactive theme color across many unrelated components in a Svelte app. Which approach is best and why?
hard
Solution
Step 1: Identify sharing scope
The theme color must be shared globally across unrelated components, not just parent-child.Step 2: Choose reactive global sharing
Writable stores provide reactive global state accessible by importing anywhere.Step 3: Evaluate other options
Context is local only, props are tedious for many components, global variables lack reactivity.Final Answer:
Use a writable store for global reactive data -> Option DQuick Check:
Stores = global reactive sharing [OK]
Hint: Use stores for global reactive data, context for local only [OK]
Common Mistakes:
- Using context for global sharing
- Passing props through many unrelated components
- Using global variables without reactivity
- Ignoring reactivity needs for theme changes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
