0
0
Svelteframework~20 mins

getContext in Svelte - Practice Problems & Coding Challenges

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