0
0
Svelteframework~20 mins

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

Choose your learning style9 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.