Discover how to share data effortlessly across your app without endless prop passing!
Why Context with stores in Svelte? - Purpose & Use Cases
Imagine you have a big app with many components, and you want to share data like user info or theme settings between them.
You try passing data down through every component manually, like handing a note from one friend to another in a long line.
This manual passing is tiring and error-prone. If you add or remove components, you must update all the data passing everywhere.
It's easy to lose track, cause bugs, or write lots of repetitive code.
Context with stores lets you share data easily across components without passing it step-by-step.
Stores hold the data, and context makes it available anywhere in the component tree automatically.
let user = {name: 'Anna'};
<Component1 user={user} />
// Component1 passes user to Component2, and so on...import { writable } from 'svelte/store'; import { setContext } from 'svelte'; const user = writable({name: 'Anna'}); setContext('user', user); // Any component can getContext('user') to access the store directly
You can build flexible apps where data flows smoothly and updates everywhere instantly without messy code.
Think of a shopping app where the cart info is needed in many places: header, product list, checkout. Context with stores shares the cart data easily so all parts stay in sync.
Manual data passing is slow and fragile.
Context with stores shares data globally in a clean way.
It makes your app easier to build and maintain.
Practice
setContext and getContext with stores?Solution
Step 1: Understand the role of
These functions allow sharing data between components without manually passing props down the tree.setContextandgetContextStep 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 DQuick Check:
Context with stores = share reactive data [OK]
- Thinking context creates new stores instead of sharing
- Confusing context with styling or HTTP handling
- Assuming context replaces all props
Solution
Step 1: Recall the correct import and usage for setting context
UsesetContextfrom 'svelte' with a key and a value (store).Step 2: Identify the correct syntax
The correct call issetContext('key', store);to provide the store under a key.Final Answer:
import { setContext } from 'svelte'; setContext('key', store); -> Option BQuick Check:
setContext with key and store = import { setContext } from 'svelte'; setContext('key', store); [OK]
- Using getContext instead of setContext to provide
- Omitting the key argument in setContext
- Using non-existent createContext function
/* Parent.svelte */
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 AQuick Check:
Store initial value logged = 5 [OK]
- Expecting undefined if context key is wrong
- Confusing setContext and getContext usage
- Assuming subscription logs nothing initially
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 CQuick Check:
getContext needs key argument [OK]
- Calling getContext without key
- Confusing getContext with setContext
- Assuming context value is not subscribable
Solution
Step 1: Provide a writable store via context in the parent
UsesetContext('count', writable(0));to share a reactive store.Step 2: Retrieve the store in children and update it
Children get the store withgetContext('count');and can callupdate()orset()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 AQuick Check:
Writable store in context updates everywhere [OK]
- Passing raw value instead of store in context
- Importing store directly instead of using context
- Assuming children cannot update shared store
Ready to prove it?
Pass the test and this pattern moves unknown
