0
0
Svelteframework~20 mins

Context key patterns in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Context Key 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 context example?

Consider a parent component that sets a context key and a child component that reads it. What will the child display?

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

/* Child.svelte */
<script>
  import { getContext } from 'svelte';
  const user = getContext('user');
</script>
<p>{user.name}</p>
AAlice
Bundefined
CError: Context key 'user' not found
Dnull
Attempts:
2 left
💡 Hint

Remember that setContext shares data with descendants using the same key.

📝 Syntax
intermediate
2:00remaining
Which option correctly sets and gets a context key in Svelte?

Identify the option that uses valid Svelte syntax to set and get a context key named 'theme'.

A
&lt;script&gt;import { setContext } from 'svelte'; setContext('theme', );&lt;/script&gt;
&lt;script&gt;import { getContext } from 'svelte'; const theme = getContext('theme');&lt;/script&gt;
B
&lt;script&gt;import { setContext } from 'svelte'; setContext(theme, 'dark');&lt;/script&gt;
&lt;script&gt;import { getContext } from 'svelte'; const theme = getContext(theme);&lt;/script&gt;
C
&lt;script&gt;import { setContext } from 'svelte'; setContext('theme');&lt;/script&gt;
&lt;script&gt;import { getContext } from 'svelte'; const theme = getContext('theme');&lt;/script&gt;
D
&lt;script&gt;import { setContext } from 'svelte'; setContext('theme', 'dark');&lt;/script&gt;
&lt;script&gt;import { getContext } from 'svelte'; const theme = getContext('theme');&lt;/script&gt;
Attempts:
2 left
💡 Hint

Check that the key is a string and the value is provided when setting context.

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

Given the parent sets context with setContext('data', 123), why does the child get undefined when calling getContext('data')?

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

/* Child.svelte */
<script>
  import { getContext } from 'svelte';
  const data = getContext('data');
  console.log(data);
</script>
AThe context key 'data' is misspelled in the child component.
BThe child is not a descendant of the parent component in the component tree.
CsetContext must be called inside onMount lifecycle to work.
DgetContext only works with numeric keys, not strings.
Attempts:
2 left
💡 Hint

Think about how Svelte context works with component hierarchy.

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

In this example, the parent updates the context value after 1 second. What does the child display before and after the update?

Svelte
/* Parent.svelte */
<script>
  import { setContext } from 'svelte';
  import { writable } from 'svelte/store';
  const count = writable(0);
  setContext('count', count);
  setTimeout(() => count.set(5), 1000);
</script>
<Child />

/* Child.svelte */
<script>
  import { getContext } from 'svelte';
  const count = getContext('count');
</script>
<p>{$count}</p>
AThrows an error because context value is not a primitive
BAlways 0, does not update
CInitially 0, then updates to 5 after 1 second
DInitially undefined, then 5 after 1 second
Attempts:
2 left
💡 Hint

Remember that Svelte stores are reactive and can be shared via context.

🧠 Conceptual
expert
3:00remaining
Which pattern is best for unique context keys in Svelte to avoid collisions?

When multiple libraries or components use context keys, how can you ensure keys do not collide?

AUse Symbols as context keys instead of strings
BPrefix all string keys with the component name
CUse numbers as keys because they are unique
DUse global variables as keys to share across apps
Attempts:
2 left
💡 Hint

Think about JavaScript features that guarantee uniqueness.