Bird
Raised Fist0
Svelteframework~20 mins

Why context shares data without prop drilling in Svelte - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Context Mastery in Svelte
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How does Svelte context avoid prop drilling?

In Svelte, context lets components share data without passing props through every level. Why does this work?

AContext stores data globally accessible to all components without passing props explicitly.
BContext automatically passes props down only to direct children components.
CContext duplicates props at each level to avoid manual passing.
DContext requires manual prop passing but caches values for performance.
Attempts:
2 left
💡 Hint

Think about how context provides data access without needing to pass props through every component.

❓ component_behavior
intermediate
2:00remaining
What will this Svelte component output using context?

Given a parent component sets context with setContext('color', 'blue') and a child uses getContext('color'), what color will the child display?

Svelte
<script>
  import { setContext, getContext } from 'svelte';
  // Parent component
  setContext('color', 'blue');
</script>

<!-- Child component -->
<script>
  import { getContext } from 'svelte';
  const color = getContext('color');
</script>
<p>{color}</p>
AError: context not found
Bundefined
Cnull
Dblue
Attempts:
2 left
💡 Hint

Remember that getContext retrieves the value set by setContext in an ancestor.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in Svelte context usage

Which option contains a syntax error when setting or getting context in Svelte?

AsetContext('user', {name: 'Anna'}); const user = getContext('user');
BsetContext(user, {name: 'Anna'}); const user = getContext('user');
CsetContext('user', {name: 'Anna'}); const user = getContext(user);
D;)'resu'(txetnoCteg = resu tsnoc ;)}'annA' :eman{ ,'resu'(txetnoCtes
Attempts:
2 left
💡 Hint

Check the types of arguments passed to setContext and getContext.

❓ state_output
advanced
2:00remaining
What is the output when context value changes in Svelte?

If a parent component sets context with a writable store and updates it, what will a child component using getContext show?

Svelte
<script>
  import { writable } from 'svelte/store';
  import { setContext, getContext } from 'svelte';

  // Parent component
  const count = writable(0);
  setContext('count', count);

  // Later update
  count.set(5);
</script>

<!-- Child component -->
<script>
  import { getContext } from 'svelte';
  const count = getContext('count');
</script>
<p>{$count}</p>
A0
Bundefined
C5
DError: $count is not defined
Attempts:
2 left
💡 Hint

Consider how Svelte stores and reactivity work with context.

🔧 Debug
expert
3:00remaining
Why does this Svelte child component fail to get context?

Given this code, why does the child component's getContext return undefined?

Svelte
<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  import { setContext, onMount } from 'svelte';
  setContext('theme', 'dark');
</script>
<Child />

<!-- Child.svelte -->
<script>
  import { getContext } from 'svelte';
  const theme = getContext('theme');
</script>
<p>{theme}</p>
AsetContext was called outside the component function, so context is not set properly.
BChild component must import setContext, not getContext.
CThe key 'theme' is misspelled in Child component.
DContext only works with stores, not strings.
Attempts:
2 left
💡 Hint

Think about when and where setContext should be called inside a Svelte component.

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

  1. Step 1: Understand prop drilling

    Prop drilling means passing data through many intermediate components even if they don't need it.
  2. Step 2: How context helps

    Context lets a parent share data directly with any child, skipping intermediate components.
  3. Final Answer:

    Because it allows sharing data directly between components without passing props through every level -> Option A
  4. 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

  1. Step 1: Identify context provider function

    In Svelte, setContext is used in a parent to provide data.
  2. Step 2: Differentiate from consumer function

    getContext is used in children to access that data, not to provide it.
  3. Final Answer:

    setContext -> Option B
  4. 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

  1. Step 1: Understand setContext usage

    The parent sets context with key 'user' and value { name: 'Anna' }.
  2. Step 2: Child accesses context

    The child calls getContext('user') and gets the object, then logs user.name which is 'Anna'.
  3. Final Answer:

    Anna -> Option A
  4. 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

Solution

  1. Step 1: Compare keys in setContext and getContext

    Parent uses 'theme' (lowercase), child uses 'Theme' (capital T). Keys are case-sensitive.
  2. Step 2: Effect of key mismatch

    Because keys differ, getContext returns undefined, causing unexpected behavior.
  3. Final Answer:

    The key used in getContext has different capitalization than setContext -> Option D
  4. Quick Check:

    Context keys are case-sensitive = A [OK]
Hint: Match context keys exactly, including case [OK]
Common Mistakes:
  • Thinking setContext must be in onMount
  • Believing getContext can't be used in children
  • Assuming keys can be numbers only
5. In a nested Svelte component tree, how does context help when a deeply nested child needs data from a distant ancestor without prop drilling?
/* Grandparent.svelte */


/* Parent.svelte */



/* Child.svelte */

Why is this approach better than passing props through Parent.svelte?
hard
A. Because context automatically updates props in Parent.svelte
B. Because context duplicates data in every component for safety
C. Because context allows Child.svelte to access data directly without Parent.svelte needing to pass props
D. Because context requires less code but still passes props through Parent.svelte

Solution

  1. Step 1: Understand nested component data flow

    Without context, data must be passed through Parent.svelte as props even if it doesn't use it.
  2. Step 2: How context simplifies this

    Context lets Child.svelte get data directly from Grandparent.svelte, skipping Parent.svelte entirely.
  3. Final Answer:

    Because context allows Child.svelte to access data directly without Parent.svelte needing to pass props -> Option C
  4. Quick Check:

    Context skips intermediate props = B [OK]
Hint: Context skips intermediate components, direct data access [OK]
Common Mistakes:
  • Thinking context updates props automatically
  • Believing context duplicates data everywhere
  • Assuming context still passes props through all components
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