0
0
Svelteframework~20 mins

setContext in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Context Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Svelte code using setContext and getContext?

Consider a parent component that uses setContext to share a value, and a child component that uses getContext to access it. What will the child display?

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

/* Child.svelte */
<script>
  import { getContext } from 'svelte';
  const color = getContext('color');
</script>
<p>{color}</p>
AThe child component throws a runtime error
BThe child component displays: undefined
CThe child component displays: blue
DThe child component displays: null
Attempts:
2 left
💡 Hint

Remember that setContext shares data with descendants, and getContext retrieves it.

state_output
intermediate
2:00remaining
What is the value of the variable after using setContext and getContext with reactive stores?

In Svelte, if a parent sets a writable store in context and the child subscribes to it, what will the child display after the store updates?

Svelte
/* Parent.svelte */
<script>
  import { setContext } from 'svelte';
  import { writable } from 'svelte/store';
  import Child from './Child.svelte';

  const count = writable(0);
  setContext('count', count);

  setTimeout(() => count.set(5), 100);
</script>
<Child />

/* Child.svelte */
<script>
  import { getContext } from 'svelte';
  const count = getContext('count');
  let value = 0;
  const unsubscribe = count.subscribe(v => value = v);
  // Optional: unsubscribe on destroy
</script>
<p>{value}</p>
AInitially 0, then updates to 5 after 100ms
BInitially 5, then updates to 0 after 100ms
CAlways 0, does not update
DThrows an error because stores cannot be passed via context
Attempts:
2 left
💡 Hint

Writable stores are reactive and can be shared via context.

📝 Syntax
advanced
2:00remaining
Which option correctly uses setContext in a Svelte component?

Choose the code snippet that correctly sets a context value in a Svelte component.

A<script>import { setContext } from 'svelte'; setContext('key' 123);</script>
B<script>import { setContext } from 'svelte'; setContext('key', 123);</script>
C<script>import { setContext } from 'svelte'; setContext('key');</script>
D<script>import { setContext } from 'svelte'; setContext(123, 'key');</script>
Attempts:
2 left
💡 Hint

Check the function signature: setContext(key, value).

🔧 Debug
advanced
2:00remaining
Why does this Svelte child component get undefined from getContext?

A parent sets context with setContext('user', {name: 'Anna'}). The child calls getContext('user') but gets undefined. What is the most likely reason?

Svelte
/* Parent.svelte */
<script>
  import { setContext } from 'svelte';
  import Child from './Child.svelte';
  setContext('user', {name: 'Anna'});
</script>
<Child />

/* Child.svelte */
<script>
  import { getContext } from 'svelte';
  const user = getContext('user');
</script>
<p>{user?.name}</p>
AThe child is not a descendant of the parent component that called setContext
BsetContext must be called inside onMount lifecycle
CThe key 'user' is misspelled in the child component
DgetContext only works with primitive values, not objects
Attempts:
2 left
💡 Hint

Context only works down the component tree.

🧠 Conceptual
expert
3:00remaining
How does Svelte's setContext differ from React's Context API in component communication?

Choose the statement that best describes a key difference between Svelte's setContext and React's Context API.

ASvelte's setContext automatically updates all components on data change, React's Context requires manual subscription
BSvelte's setContext shares data only with direct descendants, React's Context can be accessed by any nested component regardless of depth
CSvelte's setContext requires a store to share data, React's Context only shares static values
DSvelte's setContext is synchronous and scoped to a component subtree, React's Context uses a Provider component and can trigger re-renders on consumers
Attempts:
2 left
💡 Hint

Think about how data flows and updates happen in both frameworks.