Discover how to effortlessly share data in Svelte without tangled props or confusing updates!
Context vs stores decision in Svelte - When to Use Which
Imagine you have a Svelte app where many components need to share data like user info or theme settings. You try passing props down through many layers or manually updating each component.
Passing props through many layers is tiring and error-prone. If you update data, you must manually notify every component. This leads to messy code and bugs.
Svelte's context and stores let you share data easily. Context passes data down the tree without props, while stores provide reactive shared state accessible anywhere.
let user = {name: 'Anna'};
<Component1 user={user} />
// Component1 passes user to Component2, and so on...import { writable } from 'svelte/store'; export const user = writable({name: 'Anna'}); // Any component can import and subscribe to the user store
You can share and react to data changes smoothly across your app without messy prop chains or manual updates.
In a shopping app, the cart data is shared across header, product list, and checkout components. Using stores or context keeps all parts in sync instantly.
Passing data manually through props is slow and error-prone.
Context shares data down the component tree without props.
Stores provide reactive shared state accessible anywhere.
Practice
context instead of stores?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]
- 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
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]
- 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
import { writable } from 'svelte/store';
const count = writable(0);
count.subscribe(value => console.log(value));
count.set(5);
count.update(n => n + 1);
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]
- Ignoring initial subscription call
- Mixing order of set and update
- Assuming update starts from 0 after set
- Confusing subscribe behavior with events
import { getContext } from 'svelte';
const value = getContext('user');
console.log(value);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]
- Calling getContext in module scope
- Confusing getContext with setContext usage
- Assuming string keys must be objects
- Expecting getContext to have default value argument
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]
- Using context for global sharing
- Passing props through many unrelated components
- Using global variables without reactivity
- Ignoring reactivity needs for theme changes
Ready to prove it?
Pass the test and this pattern moves unknown
