📖 Scenario: You are building a simple Svelte app where a parent component shares a theme color with its child components using context keys.
🎯 Goal: Learn how to create and use context keys in Svelte to pass data from a parent component to child components without props.
📋 What You'll Learn
Create a unique context key using Symbol()
Set context in the parent component using setContext
Get context in the child component using getContext
Display the shared theme color in the child component
💡 Why This Matters
🌍 Real World
Context keys let you share data like themes, user info, or settings between components without passing props through many layers.
💼 Career
Understanding context keys is important for building scalable Svelte apps where components need to share data cleanly and efficiently.
Progress0 / 4 steps
1
Create a unique context key
In a new Svelte component file, create a constant called themeKey and assign it the value Symbol('theme').
Svelte
Hint
Use const themeKey = Symbol('theme') to create a unique key for context.
2
Set context in the parent component
Import setContext from 'svelte'. Then use setContext(themeKey, 'darkblue') to share the theme color 'darkblue' using the themeKey.
Svelte
Hint
Remember to import setContext and call it with the key and value.
3
Get context in the child component
In a child Svelte component, import getContext from 'svelte'. Then create a constant called theme and assign it the value returned by getContext(themeKey).
Svelte
Hint
Use getContext(themeKey) to access the shared theme value.
4
Display the theme color in the child component
In the child component's markup, add a <div> that shows the text Theme color: {theme}. This will display the shared theme color from context.
Svelte
Hint
Use a <div> with {theme} inside to show the color.
Practice
(1/5)
1. What is the main reason to use Symbol as a context key in Svelte?
easy
A. To ensure the key is unique and avoid conflicts between components
B. To make the key a string for easier debugging
C. To allow the key to be reused as a CSS class name
D. To automatically serialize the key for storage
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 A
B. Using string 'themeKey' instead of Symbol key in setContext
C. Using getContext before setContext is called
D. Not exporting themeKey from key.js
Solution
Step 1: Check setContext key usage
Parent uses string 'themeKey' instead of the Symbol themeKey imported 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 B
Quick Check:
Key mismatch causes undefined context [OK]
Hint: Always use the same Symbol key in setContext and getContext [OK]
Common Mistakes:
Mixing string and Symbol keys
Assuming string keys work like Symbols
Not importing the shared key properly
5. You want to share a theme object between multiple nested components using context keys. Which pattern is best to avoid key conflicts and ensure consistent usage?
hard
A. Create a new Symbol key in each component separately
B. Use a plain string key like 'theme' directly in setContext and getContext calls
C. Define and export a unique Symbol key in a separate file, then import it in all components
D. Use a global variable without context to share the theme
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 C
Quick Check:
Shared Symbol key file = consistent context [OK]
Hint: Keep keys in one file, import everywhere to avoid conflicts [OK]
Common Mistakes:
Using different keys in different components
Relying on strings that may clash
Avoiding context and using globals instead
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.