Context with stores lets you share data easily between components without passing props everywhere.
Context with stores in Svelte
import { writable } from 'svelte/store'; import { setContext, getContext } from 'svelte'; // Create a store const count = writable(0); // Provide context in a parent component setContext('key', count); // Access context in a child component const count = getContext('key');
setContext shares data down the component tree.
getContext retrieves that data in child components.
import { writable } from 'svelte/store'; import { setContext } from 'svelte'; const theme = writable('light'); setContext('theme', theme);
import { getContext } from 'svelte'; const theme = getContext('theme'); $: console.log($theme);
import { writable } from 'svelte/store'; import { setContext, getContext } from 'svelte'; // Parent.svelte const user = writable({ name: 'Anna' }); setContext('user', user); // Child.svelte const user = getContext('user'); $: console.log($user.name);
The parent component creates a count store and shares it via context. It also has a button to increase the count. The child component gets the count store from context and shows its current value. When you click the button, the count updates and the child shows the new value.
<!-- Parent.svelte --> <script> import { writable } from 'svelte/store'; import { setContext } from 'svelte'; const count = writable(0); setContext('count', count); function increment() { count.update(n => n + 1); } </script> <button on:click={increment}>Increment</button> <slot /> <!-- Child.svelte --> <script> import { getContext } from 'svelte'; const count = getContext('count'); </script> <p>Count value: {$count}</p>
Context only works between parent and child components, not across unrelated components.
Stores in context stay reactive, so changes update all components using them.
Use unique keys for context to avoid conflicts.
Context with stores shares reactive data easily between components.
Use setContext in a parent and getContext in children.
This avoids passing props through many layers.
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
