0
0
Svelteframework~20 mins

Context API vs stores in Svelte - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Context & Stores Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to use Context API vs Stores in Svelte?

In Svelte, both Context API and stores help share data. Which situation best fits using the Context API instead of stores?

AReplacing all local component state with a single global store
BSharing reactive data globally across many unrelated components
CManaging global state that updates frequently and triggers UI changes everywhere
DSharing data deeply between nested components without needing reactivity outside those components
Attempts:
2 left
💡 Hint

Think about when you want to avoid global reactivity and only pass data down the component tree.

component_behavior
intermediate
2:00remaining
What happens when a Svelte store updates?

Given a Svelte writable store used in multiple components, what happens when the store's value changes?

Svelte
import { writable } from 'svelte/store';
const count = writable(0);

// Component A and Component B both subscribe to count

count.set(5);
AOnly the component that called set() re-renders
BNo components re-render until manually refreshed
CAll components subscribed to the store re-render with the new value
DOnly components that read the store inside onMount re-render
Attempts:
2 left
💡 Hint

Stores in Svelte are reactive and notify all subscribers on change.

🔧 Debug
advanced
2:00remaining
Why does this Context API example fail to share data?

Consider this Svelte code using Context API. Why does the child component not receive the context value?

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

  // Parent.svelte
  setContext('user', { name: 'Alice' });
</script>

<!-- Child.svelte -->
<script>
  const user = getContext('user');
</script>
<p>{user.name}</p>
AContext API only works with stores, not plain objects
BsetContext must be called inside a component script, not at module top-level
CgetContext key must be a Symbol, not a string
DChild component must import setContext instead of getContext
Attempts:
2 left
💡 Hint

Check where setContext is called in the component lifecycle.

📝 Syntax
advanced
2:00remaining
Which Svelte store syntax correctly creates a readable store with a cleanup function?

Choose the correct syntax to create a readable store that starts a timer and cleans up on unsubscribe.

A
import { readable } from 'svelte/store';
const time = readable(new Date(), set =&gt; {
  const interval = setInterval(() =&gt; set(new Date()), 1000);
  return () =&gt; clearInterval(interval);
});
B
import { writable } from 'svelte/store';
const time = writable(new Date(), set =&gt; {
  const interval = setInterval(() =&gt; set(new Date()), 1000);
  return () =&gt; clearInterval(interval);
});
C
import { readable } from 'svelte/store';
const time = readable(new Date(), () =&gt; {
  const interval = setInterval(() =&gt; set(new Date()), 1000);
  return () =&gt; clearInterval(interval);
});
D
import { readable } from 'svelte/store';
const time = readable(new Date(), set =&gt; {
  setInterval(() =&gt; set(new Date()), 1000);
});
Attempts:
2 left
💡 Hint

Remember the readable store callback receives a set function and must return a cleanup function.

state_output
expert
2:00remaining
What is the output after updating a nested store value in Svelte?

Given this Svelte writable store holding an object, what is the output after updating a nested property incorrectly?

Svelte
import { writable } from 'svelte/store';

const user = writable({ name: 'Bob', age: 30 });

user.update(u => {
  u.age = 31;
  return u;
});

let currentAge;
user.subscribe(value => currentAge = value.age)();

console.log(currentAge);
A31
B30
Cundefined
DThrows a TypeError
Attempts:
2 left
💡 Hint

Consider how Svelte detects changes in store values and if mutating nested objects works.