Bird
Raised Fist0
Svelteframework~20 mins

getContext in Svelte - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Svelte getContext Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this Svelte component output when using getContext?

Consider a parent component that sets a context value and a child component that uses getContext to access it. What will the child component display?

Svelte
/* Parent.svelte */
<script>
  import Child from './Child.svelte';
  import { setContext } from 'svelte';
  setContext('color', 'blue');
</script>
<Child />

/* Child.svelte */
<script>
  import { getContext } from 'svelte';
  const color = getContext('color');
</script>
<p>The color is {color}</p>
ARuntime error: getContext called outside component
BThe color is undefined
CThe color is null
DThe color is blue
Attempts:
2 left
💡 Hint

Remember that setContext shares data with child components using the same key.

❓ state_output
intermediate
1:30remaining
What is the value of variable 'theme' after running this Svelte code?

Given this Svelte component code, what is the value of theme?

Svelte
<script>
  import { getContext } from 'svelte';
  const theme = getContext('theme') ?? 'light';
</script>
Aundefined
B'dark'
C'light'
DThrows ReferenceError
Attempts:
2 left
💡 Hint

If no context is set for the key, the fallback value after ?? is used.

📝 Syntax
advanced
1:30remaining
Which option correctly uses getContext in a Svelte component?

Choose the code snippet that correctly uses getContext to retrieve a value in a Svelte component.

A<script>import { getContext } from 'svelte'; const value = getContext('key');</script>
B<script>import { getContext } from 'svelte'; const value = getContext();</script>
C<script>import { getContext } from 'svelte'; const value = getContext(key);</script>
D<script>import { getContext } from 'svelte'; const value = getContext['key'];</script>
Attempts:
2 left
💡 Hint

getContext requires a string key as an argument.

🔧 Debug
advanced
1:30remaining
What error occurs when calling getContext with a key not set by any parent?

What happens if a Svelte component calls getContext('missingKey') but no parent component has set this context?

Svelte
<script>
  import { getContext } from 'svelte';
  const value = getContext('missingKey');
</script>
AThrows ReferenceError
BReturns undefined without error
CThrows TypeError
DReturns null
Attempts:
2 left
💡 Hint

Check what getContext returns if no context is found.

🧠 Conceptual
expert
2:00remaining
Why use getContext instead of props in Svelte?

Which reason best explains why a developer would use getContext instead of passing data via props?

ATo share data deeply without passing props through every intermediate component
BTo allow components to modify parent state directly
CTo make components re-render faster by avoiding props
DTo automatically bind events between components
Attempts:
2 left
💡 Hint

Think about how data flows through nested components.

Practice

(1/5)
1. What is the main purpose of getContext in Svelte?
easy
A. To access data provided by a parent component without passing props
B. To create a new reactive variable inside a component
C. To define a component's local state
D. To listen for DOM events inside a component

Solution

  1. Step 1: Understand the role of getContext

    getContext is used to access data that a parent component has shared using setContext.

  2. Step 2: Compare with other options

    Other options describe unrelated tasks like local state or event listening, which getContext does not do.

  3. Final Answer:

    To access data provided by a parent component without passing props -> Option A
  4. Quick Check:

    getContext accesses parent data = B [OK]
Hint: Remember: getContext reads shared parent data [OK]
Common Mistakes:
  • Confusing getContext with local state creation
  • Thinking getContext listens to events
  • Assuming getContext sets data instead of getting
2. Which of the following is the correct syntax to retrieve a context value with key 'user' in a Svelte component?
easy
A. const user = getContext({ key: 'user' });
B. const user = getContext('user');
C. const user = getContext(user);
D. const user = setContext('user');

Solution

  1. Step 1: Recall getContext syntax

    getContext takes a string key to retrieve the shared value.

  2. 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.

  3. Final Answer:

    const user = getContext('user'); -> Option B
  4. Quick Check:

    getContext('key') syntax = D [OK]
Hint: Use string keys in quotes with getContext [OK]
Common Mistakes:
  • Using setContext instead of getContext
  • Passing variables or objects instead of string keys
  • Missing quotes around the key
3. Given the parent component sets context with setContext('theme', 'dark'), what will the child component log with console.log(getContext('theme'))?
medium
A. 'dark'
B. 'light'
C. undefined
D. null

Solution

  1. Step 1: Understand setContext and getContext relation

    The parent sets the key 'theme' with value 'dark'. The child uses getContext with the same key.

  2. Step 2: Determine the logged value

    Since the keys match, getContext returns 'dark'. Other options are incorrect values or undefined.

  3. Final Answer:

    'dark' -> Option A
  4. Quick Check:

    Matching keys return set value = A [OK]
Hint: Matching keys return parent's setContext value [OK]
Common Mistakes:
  • Expecting default or unrelated values
  • Using different keys causing undefined
  • Confusing setContext and getContext roles
4. What is the error in this child component code snippet?
const data = getContext();
medium
A. getContext must be imported from 'svelte/store'
B. getContext cannot be used in child components
C. getContext requires two arguments
D. Missing key argument in getContext call

Solution

  1. Step 1: Check getContext usage

    getContext requires a key argument to specify which context to retrieve.

  2. Step 2: Evaluate the code snippet

    The snippet calls getContext without any argument, causing an error.

  3. Final Answer:

    Missing key argument in getContext call -> Option D
  4. Quick Check:

    getContext needs a key argument = A [OK]
Hint: Always pass a key string to getContext [OK]
Common Mistakes:
  • Calling getContext without arguments
  • Thinking getContext needs two arguments
  • Importing getContext from wrong module
5. In a nested component structure, if a grandchild calls getContext('settings') but only the grandparent used setContext('settings', {mode: 'auto'}), what will the grandchild receive?
hard
A. An error because only the direct parent can provide context
B. undefined because context is not passed through intermediate components
C. The object {mode: 'auto'} from the grandparent
D. An empty object {} because context resets at each level

Solution

  1. Step 1: Understand context propagation in Svelte

    Context set by any ancestor is accessible by all descendants, not just direct children.

  2. Step 2: Apply to the nested structure

    The grandchild can access the 'settings' context set by the grandparent, receiving the object {mode: 'auto'}.

  3. Final Answer:

    The object {mode: 'auto'} from the grandparent -> Option C
  4. Quick Check:

    Context flows down all descendants = C [OK]
Hint: Context flows down all descendants, not just direct children [OK]
Common Mistakes:
  • Thinking only direct parent context is accessible
  • Expecting undefined if intermediate components don't set context
  • Assuming context resets at each component level
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard