📖 Scenario: Imagine you have a family tree app where the grandparent wants to share a message with the grandchild. Instead of passing the message through the parent, we use context to share it directly.
🎯 Goal: Build a simple Svelte app that uses context to share a message from a grandparent component to a grandchild component without passing props through the parent.
📋 What You'll Learn
Create a Grandparent.svelte component that sets a context value called message with the text 'Hello from Grandparent!'
Create a Parent.svelte component that does not receive or pass any props
Create a Grandchild.svelte component that reads the message from context and displays it
Use Svelte's setContext and getContext functions properly
💡 Why This Matters
🌍 Real World
Context sharing is useful in apps where many components need access to the same data, like user info or theme settings, without passing props through every level.
💼 Career
Understanding context is important for building clean, maintainable component-based apps in Svelte and other frameworks.
Progress0 / 4 steps
1
Set up the Grandparent component with context
In Grandparent.svelte, import setContext from 'svelte'. Create a variable called message with the value 'Hello from Grandparent!'. Use setContext with the key 'message' and the message variable to share this data. Add the <Parent /> component inside the markup.
Svelte
Hint
Remember to import setContext and call it before the markup.
2
Create the Parent component without props
In Parent.svelte, import and include the Grandchild component in the markup. Do not pass any props to Grandchild or receive any props in Parent.
Svelte
Hint
This component just passes through the child without props.
3
Read the context in Grandchild component
In Grandchild.svelte, import getContext from 'svelte'. Use getContext with the key 'message' to get the shared message. Display the message inside a <p> tag.
Svelte
Hint
Use getContext to access the message set by the grandparent.
4
Combine components in App.svelte
In App.svelte, import the Grandparent component and include it in the markup to render the full component tree.
Svelte
Hint
This is the root component that starts the app.
Practice
(1/5)
1. Why does Svelte's context API help avoid prop drilling?
easy
A. Because it allows sharing data directly between components without passing props through every level
B. Because it automatically updates the DOM without any code
C. Because it replaces the need for events between components
D. Because it forces all components to use the same state variable
Solution
Step 1: Understand prop drilling
Prop drilling means passing data through many intermediate components even if they don't need it.
Step 2: How context helps
Context lets a parent share data directly with any child, skipping intermediate components.
Final Answer:
Because it allows sharing data directly between components without passing props through every level -> Option A
Quick Check:
Context avoids prop drilling = D [OK]
Hint: Context shares data directly, skipping intermediate props [OK]
Common Mistakes:
Thinking context replaces events
Believing context auto-updates DOM
Assuming all components share one state variable
2. Which Svelte function is used to provide context data to child components?
easy
A. getContext
B. setContext
C. useContext
D. provideContext
Solution
Step 1: Identify context provider function
In Svelte, setContext is used in a parent to provide data.
Step 2: Differentiate from consumer function
getContext is used in children to access that data, not to provide it.
Final Answer:
setContext -> Option B
Quick Check:
Provide context = setContext [OK]
Hint: Use setContext to provide data, getContext to consume [OK]
Common Mistakes:
Confusing getContext as provider
Using React's useContext in Svelte
Assuming provideContext exists
3. What will be logged by the child component in this Svelte code?
/* Parent.svelte */
/* Child.svelte */
medium
A. Anna
B. undefined
C. Error: getContext not found
D. null
Solution
Step 1: Understand setContext usage
The parent sets context with key 'user' and value { name: 'Anna' }.
Step 2: Child accesses context
The child calls getContext('user') and gets the object, then logs user.name which is 'Anna'.
Final Answer:
Anna -> Option A
Quick Check:
getContext returns provided data = Anna [OK]
Hint: getContext returns parent's data by key [OK]
Common Mistakes:
Expecting undefined if not passed as prop
Confusing getContext with props
Thinking getContext throws error if key missing
4. Identify the error in this Svelte code using context:
/* Parent.svelte */
/* Child.svelte */
medium
A. Context keys must be numbers, not strings
B. setContext must be called inside onMount lifecycle
C. getContext cannot be used in child components
D. The key used in getContext has different capitalization than setContext