📖 Scenario: You are building a simple Svelte app where a parent component shares a message with its child component using setContext and getContext.
🎯 Goal: Create a parent component that uses setContext to provide a message string. Then create a child component that uses getContext to receive and display that message.
📋 What You'll Learn
Create a parent Svelte component with a message string
Use setContext in the parent to share the message
Create a child Svelte component
Use getContext in the child to access the message
Display the message inside the child component
💡 Why This Matters
🌍 Real World
Sharing data between components without passing props directly is common in apps with nested components, like theme settings or user info.
💼 Career
Understanding setContext and getContext is important for building scalable Svelte apps where components need to share data cleanly.
Progress0 / 4 steps
1
Create the parent component with a message
In a Svelte component file, create a message variable with the exact string 'Hello from parent!'.
Svelte
Hint
Use let message = 'Hello from parent!' to create the message variable.
2
Use setContext to share the message
Import setContext from 'svelte' and use setContext with the key 'msg' and the message variable to share it.
Svelte
Hint
Use import { setContext } from 'svelte' and then setContext('msg', message).
3
Create a child component that gets the message
In a new Svelte component file, import getContext from 'svelte'. Use getContext('msg') to get the message and store it in a variable called receivedMessage.
Svelte
Hint
Use let receivedMessage = getContext('msg') to get the message from context.
4
Display the received message in the child component
Add markup to the child component to display the receivedMessage variable inside a <p> tag.
Svelte
Hint
Use <p>{receivedMessage}</p> to show the message.
Practice
(1/5)
1. What is the main purpose of setContext in Svelte?
easy
A. To style components using CSS variables
B. To fetch data from an API inside a component
C. To create a reactive variable inside a component
D. To share data or functions from a parent component to its children without using props
Solution
Step 1: Understand the role of setContext
setContext is used to share data or functions from a parent component to its children without passing props explicitly.
Step 2: Compare with other options
Options A, B, and C describe unrelated tasks like styling, fetching data, or creating reactive variables, which are not the purpose of setContext.
Final Answer:
To share data or functions from a parent component to its children without using props -> Option D
Quick Check:
Sharing data without props = D [OK]
Hint: Remember: setContext shares data down without props [OK]
Common Mistakes:
Confusing setContext with fetching data
Thinking setContext creates reactive variables
Mixing setContext with styling methods
2. Which of the following is the correct syntax to use setContext in a Svelte component?
easy
A. setContext('key');
B. setContext('key', value);
C. setContext(key, value);
D. setContext(value, 'key');
Solution
Step 1: Recall the setContext function signature
The function takes two arguments: a unique key (usually a variable or symbol) and a value to share.
Step 2: Analyze the options
setContext(key, value); correctly uses setContext(key, value); where key is a variable or symbol. setContext('key', value); uses a string literal which is discouraged because keys should be unique and not collide. setContext('key'); misses the value argument. setContext(value, 'key'); reverses the arguments.
Final Answer:
setContext(key, value); -> Option C
Quick Check:
Correct syntax is setContext(key, value) [OK]
Hint: Use a variable or symbol as key, then value: setContext(key, value) [OK]
Common Mistakes:
Using string literals as keys instead of variables or symbols
C. setContext must be imported from 'svelte/store'
D. Key must be a string, not a Symbol
Solution
Step 1: Review setContext usage
The setContext function requires two arguments: a key and a value to share.
Step 2: Check the code for missing arguments
The code calls setContext(key); with only one argument, missing the value argument.
Final Answer:
Missing value argument in setContext call -> Option A
Quick Check:
setContext needs key and value [OK]
Hint: Always provide both key and value to setContext [OK]
Common Mistakes:
Forgetting the value argument
Thinking Symbol keys are invalid
Importing setContext from wrong module
5. You want to share a function from a parent to multiple nested child components using setContext. Which approach correctly ensures all children can access the function safely?
hard
A. Use a string key like 'sharedFunc' directly in setContext and getContext calls without importing
B. Define a unique Symbol key in a separate module, import it in parent and children, then use setContext(key, func) in parent and getContext(key) in children
C. Pass the function as a prop to each child instead of using setContext
D. Define the function inside each child component separately
Solution
Step 1: Understand key uniqueness and sharing
Using a unique Symbol key defined in a separate module and imported ensures the same key is used in parent and children, avoiding collisions.
Step 2: Compare with other options
Use a string key like 'sharedFunc' directly in setContext and getContext calls without importing risks key collisions and is less safe. Pass the function as a prop to each child instead of using setContext works but is not using setContext. Define the function inside each child component separately duplicates code and is not sharing.
Final Answer:
Define a unique Symbol key in a separate module, import it in parent and children, then use setContext(key, func) in parent and getContext(key) in children -> Option B
Quick Check:
Unique shared key via module = A [OK]
Hint: Use a shared Symbol key imported by all components [OK]
Common Mistakes:
Using string keys without sharing the key variable
Passing functions only via props when setContext fits better
Duplicating function code in children
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.