getContext in Svelte - Step-by-Step Execution
import { setContext, getContext } from 'svelte'; // Parent.svelte setContext('color', 'blue'); // Child.svelte const color = getContext('color');
| Step | Component | Action | Key | Value | Result |
|---|---|---|---|---|---|
| 1 | Parent | setContext | color | blue | Stores 'blue' under 'color' key |
| 2 | Child | getContext | color | - | Retrieves 'blue' from context |
| 3 | Child | Use value | - | blue | Uses 'blue' as color value |
| 4 | End | - | - | - | Child has access to parent's context value |
| Variable | Start | After Parent setContext | After Child getContext | Final |
|---|---|---|---|---|
| context['color'] | undefined | 'blue' | 'blue' | 'blue' |
| color (in Child) | undefined | undefined | 'blue' | 'blue' |
getContext lets a child component access data set by a parent with setContext. Parent calls setContext(key, value). Child calls getContext(key) to retrieve value. Only children can access parent's context. If key not found, getContext returns undefined.
Practice
getContext in Svelte?Solution
Step 1: Understand the role of
getContextgetContextis used to access data that a parent component has shared usingsetContext.Step 2: Compare with other options
Other options describe unrelated tasks like local state or event listening, which
getContextdoes not do.Final Answer:
To access data provided by a parent component without passing props -> Option AQuick Check:
getContext accesses parent data = B [OK]
- Confusing getContext with local state creation
- Thinking getContext listens to events
- Assuming getContext sets data instead of getting
Solution
Step 1: Recall getContext syntax
getContexttakes a string key to retrieve the shared value.Step 2: Analyze options
const user = getContext('user'); uses the correct string key syntax. const user = setContext('user'); wrongly uses
setContext. const user = getContext(user); passes a variable instead of a string. const user = getContext({ key: 'user' }); passes an object, which is invalid.Final Answer:
const user = getContext('user'); -> Option BQuick Check:
getContext('key') syntax = D [OK]
- Using setContext instead of getContext
- Passing variables or objects instead of string keys
- Missing quotes around the key
setContext('theme', 'dark'), what will the child component log with console.log(getContext('theme'))?Solution
Step 1: Understand setContext and getContext relation
The parent sets the key 'theme' with value 'dark'. The child uses getContext with the same key.
Step 2: Determine the logged value
Since the keys match, getContext returns 'dark'. Other options are incorrect values or undefined.
Final Answer:
'dark' -> Option AQuick Check:
Matching keys return set value = A [OK]
- Expecting default or unrelated values
- Using different keys causing undefined
- Confusing setContext and getContext roles
const data = getContext();
Solution
Step 1: Check getContext usage
getContextrequires a key argument to specify which context to retrieve.Step 2: Evaluate the code snippet
The snippet calls
getContextwithout any argument, causing an error.Final Answer:
Missing key argument in getContext call -> Option DQuick Check:
getContext needs a key argument = A [OK]
- Calling getContext without arguments
- Thinking getContext needs two arguments
- Importing getContext from wrong module
getContext('settings') but only the grandparent used setContext('settings', {mode: 'auto'}), what will the grandchild receive?Solution
Step 1: Understand context propagation in Svelte
Context set by any ancestor is accessible by all descendants, not just direct children.
Step 2: Apply to the nested structure
The grandchild can access the 'settings' context set by the grandparent, receiving the object {mode: 'auto'}.
Final Answer:
The object {mode: 'auto'} from the grandparent -> Option CQuick Check:
Context flows down all descendants = C [OK]
- Thinking only direct parent context is accessible
- Expecting undefined if intermediate components don't set context
- Assuming context resets at each component level
Ready to prove it?
Pass the test and this pattern moves unknown
