0
0
Svelteframework~10 mins

getContext in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - getContext
Parent Component
Call setContext(key, value)
Child Component
Call getContext(key)
Receive value from Parent
Use value in Child
The parent sets a context value with a key, then the child retrieves it using getContext with the same key.
Execution Sample
Svelte
import { setContext, getContext } from 'svelte';

// Parent.svelte
setContext('color', 'blue');

// Child.svelte
const color = getContext('color');
Parent sets a context value 'color' to 'blue', child gets 'color' from context.
Execution Table
StepComponentActionKeyValueResult
1ParentsetContextcolorblueStores 'blue' under 'color' key
2ChildgetContextcolor-Retrieves 'blue' from context
3ChildUse value-blueUses 'blue' as color value
4End---Child has access to parent's context value
💡 Child retrieves value successfully; execution ends.
Variable Tracker
VariableStartAfter Parent setContextAfter Child getContextFinal
context['color']undefined'blue''blue''blue'
color (in Child)undefinedundefined'blue''blue'
Key Moments - 2 Insights
Why does getContext('color') return undefined if called in Parent?
Because setContext stores the value for child components, not for the same component. Execution_table row 2 shows getContext is called in Child, not Parent.
What happens if the key used in getContext does not exist?
getContext returns undefined if no matching key is found. Execution_table row 2 shows retrieval of existing key; missing keys return undefined.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value does getContext('color') return at step 2?
A'red'
B'blue'
Cundefined
Dnull
💡 Hint
Check row 2 in execution_table where getContext('color') returns the stored value.
At which step does the child component receive the context value?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table row 2 where getContext is called in Child.
If the parent sets context with key 'theme' instead of 'color', what happens when child calls getContext('color')?
AChild gets undefined
BChild gets 'theme'
CChild gets 'blue'
DError thrown
💡 Hint
Refer to variable_tracker showing context keys and values.
Concept Snapshot
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.
Full Transcript
In Svelte, getContext is used by a child component to get data from its parent. The parent uses setContext with a key and value. Then the child calls getContext with the same key to get that value. This lets components share data without passing props directly. The execution flow starts with the parent setting context, then the child retrieving it. Variables track the stored context and the child's received value. If the key is missing, getContext returns undefined. This method helps keep components connected in a clean way.