getContext in Svelte - Deep Dive
Ancestor Component │ │ (setContext provides data) ▼ Intermediate Components (do not need to pass data) │ │ ▼ Child Component │ │ (getContext retrieves data directly from ancestor) ▼ Uses shared data/function
Component Tree:
┌───────────────┐
│ Ancestor │
│ setContext(k) │
└──────┬────────┘
│
▼
┌───────────────┐
│ Intermediate │
│ (no context) │
└──────┬────────┘
│
▼
┌───────────────┐
│ Child │
│ getContext(k) │
└───────────────┘
Lookup Process:
Child calls getContext(k)
↓
Check Intermediate: no k
↓
Check Ancestor: has k → return valuePractice
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
