Prop drilling is when you pass data through many layers of components just to reach a deeply nested child component.
Click to reveal answer
beginner
How does Svelte's context API help avoid prop drilling?
Svelte's context API lets a parent component provide data that any nested child can access directly, without passing props through every intermediate component.
Click to reveal answer
intermediate
Which Svelte functions are used to share and access context data?
The setContext function shares data from a parent, and getContext lets a child component access that data.
Click to reveal answer
intermediate
Why is using context better than prop drilling for deeply nested components?
Context reduces code clutter and makes components simpler by avoiding the need to pass props through components that don't use the data.
Click to reveal answer
advanced
Can context data in Svelte be reactive and update child components automatically?
Yes, if the context data is a Svelte store, child components accessing it will reactively update when the store changes.
Click to reveal answer
What problem does Svelte's context API solve?
APassing data without prop drilling
BStyling components globally
CHandling user events
DManaging component lifecycle
✗ Incorrect
Svelte's context API allows sharing data directly with nested components, avoiding prop drilling.
Which function sets data in Svelte's context?
AgetContext
BsetContext
CuseContext
DcreateContext
✗ Incorrect
setContext is used to provide data to the context in a parent component.
If a child component needs data from context, which function does it use?
AgetContext
BuseState
CsetContext
Dsubscribe
✗ Incorrect
getContext retrieves data from the context inside a child component.
What happens if you try to access context data in a component without a matching setContext?
AIt returns an empty object
BIt throws an error
CIt returns null
DIt returns undefined
✗ Incorrect
If no context is set for the key, getContext returns undefined.
How can context data be reactive in Svelte?
ABy using lifecycle hooks
BBy using plain objects
CBy using a Svelte store as context data
DBy passing props instead
✗ Incorrect
Using a Svelte store in context makes data reactive and updates components automatically.
Explain how Svelte's context API helps avoid prop drilling and why this is useful.
Think about passing data through many layers versus direct access.
You got /4 concepts.
Describe how you would make context data reactive in Svelte and why that matters.
Consider what happens when data changes and components update.
You got /3 concepts.
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