Context key patterns help you share data between components without passing props all the time. It makes your code cleaner and easier to manage.
Context key patterns in Svelte
import { setContext, getContext } from 'svelte'; // In parent component setContext(key, value); // In child component const value = getContext(key);
The key can be any value, but using a Symbol is best to avoid conflicts.
Only components inside the parent can access the context with the same key.
// Using a string key import { setContext, getContext } from 'svelte'; // Parent.svelte setContext('color', 'blue'); // Child.svelte const color = getContext('color');
// Using a Symbol key import { setContext, getContext } from 'svelte'; const THEME = Symbol('theme'); // Parent.svelte setContext(THEME, { background: 'white' }); // Child.svelte const theme = getContext(THEME);
This example shows how to share user data using a Symbol key stored in a separate file. This avoids key mismatches and allows the child component to access the user info correctly.
<!-- Parent.svelte --> <script> import { setContext } from 'svelte'; import Child from './Child.svelte'; import { USER } from './userKey.js'; const user = { name: 'Alice', loggedIn: true }; setContext(USER, user); </script> <h1>Welcome to the app</h1> <Child /> <!-- Child.svelte --> <script> import { getContext } from 'svelte'; import { USER } from './userKey.js'; const user = getContext(USER); </script> <p>User name: {user.name}</p> <p>Logged in: {user.loggedIn ? 'Yes' : 'No'}</p> <!-- userKey.js --> export const USER = Symbol('user');
Always use the same key instance (like a shared Symbol) in both setContext and getContext.
Context only works downward in the component tree, not upward or sideways.
Context is great for global-like data but avoid overusing it for everything.
Context key patterns let you share data easily between components without props.
Use Symbols as keys to avoid conflicts and keep keys unique.
Keep your keys in shared files to use them consistently across components.
Practice
Symbol as a context key in Svelte?Solution
Step 1: Understand context keys purpose
Context keys identify data shared between components without props.Step 2: Why use Symbol?
Symbols are unique values, preventing accidental key collisions.Final Answer:
To ensure the key is unique and avoid conflicts between components -> Option AQuick Check:
Unique keys = Symbol usage [OK]
- Using strings that can clash with other keys
- Thinking keys must be strings for context
- Reusing keys across unrelated components
Solution
Step 1: Check correct Symbol creation
Symbols are created withSymbol('description'), not withnew.Step 2: Check export syntax
Exporting a constant withexport const key = Symbol('myKey');is correct and common.Final Answer:
export const key = Symbol('myKey'); -> Option DQuick Check:
Correct Symbol export = export const key = Symbol('myKey'); [OK]
- Using string instead of Symbol for key
- Using new Symbol() which is invalid
- Exporting default instead of named export
Child.svelte component?/* key.js */
export const userKey = Symbol('user');
/* Parent.svelte */
import { setContext } from 'svelte';
import { userKey } from './key.js';
setContext(userKey, { name: 'Alice' });
/* Child.svelte */
import { getContext } from 'svelte';
import { userKey } from './key.js';
const user = getContext(userKey);
console.log(user.name);Solution
Step 1: Understand setContext and getContext usage
Parent sets context with keyuserKeyand value{ name: 'Alice' }.Step 2: Child retrieves context using same key
Child importsuserKeyand callsgetContext(userKey), getting the object.Final Answer:
Alice -> Option AQuick Check:
Matching keys return value = Alice [OK]
- Using different keys causing undefined
- Forgetting to import the shared key
- Expecting string keys instead of Symbol keys
/* key.js */
export const themeKey = Symbol('theme');
/* Parent.svelte */
import { setContext } from 'svelte';
import { themeKey } from './key.js';
setContext('themeKey', 'dark');
/* Child.svelte */
import { getContext } from 'svelte';
import { themeKey } from './key.js';
const theme = getContext(themeKey);
console.log(theme);Solution
Step 1: Check setContext key usage
Parent uses string 'themeKey' instead of the SymbolthemeKeyimported from key.js.Step 2: Understand key mismatch effect
Child uses Symbol key, so getContext returns undefined because keys differ.Final Answer:
Using string 'themeKey' instead of Symbol key in setContext -> Option BQuick Check:
Key mismatch causes undefined context [OK]
- Mixing string and Symbol keys
- Assuming string keys work like Symbols
- Not importing the shared key properly
Solution
Step 1: Understand key uniqueness and sharing
Using a shared Symbol key exported from one file ensures all components use the exact same key.Step 2: Avoid conflicts and inconsistencies
Using strings or separate Symbols risks conflicts or mismatches causing bugs.Final Answer:
Define and export a unique Symbol key in a separate file, then import it in all components -> Option CQuick Check:
Shared Symbol key file = consistent context [OK]
- Using different keys in different components
- Relying on strings that may clash
- Avoiding context and using globals instead
Ready to prove it?
Pass the test and this pattern moves unknown
