Bird
Raised Fist0
Svelteframework~20 mins

Context API vs stores in Svelte - Practice Questions

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

Practice

(1/5)
1. Which statement best describes the main difference between Svelte's Context API and stores?
easy
A. Context API is used for styling components, stores manage component lifecycle.
B. Stores are only for passing data from parent to child, Context API is for global state.
C. Context API shares data only between parent and child components, while stores provide global reactive state accessible anywhere.
D. Stores and Context API are identical in functionality and usage.

Solution

  1. Step 1: Understand Context API scope

    Context API shares data only between parent and its children components, not globally.
  2. Step 2: Understand stores scope

    Stores provide reactive global state accessible from any component in the app.
  3. Final Answer:

    Context API shares data only between parent and child components, while stores provide global reactive state accessible anywhere. -> Option C
  4. Quick Check:

    Context API = parent-child, stores = global state [OK]
Hint: Context API is local parent-child, stores are global [OK]
Common Mistakes:
  • Thinking stores only pass data down like props
  • Confusing Context API with global state management
  • Assuming stores are for styling or lifecycle
2. Which of the following is the correct way to set a value in a Svelte writable store?
easy
A. store.set(newValue);
B. store.value = newValue;
C. store.update(newValue);
D. store.change(newValue);

Solution

  1. Step 1: Recall writable store methods

    Svelte writable stores have a set() method to assign a new value.
  2. Step 2: Check other options

    store.value is not valid, update() takes a function, change() does not exist.
  3. Final Answer:

    store.set(newValue); -> Option A
  4. Quick Check:

    Use set() to assign store value [OK]
Hint: Use store.set(value) to update writable stores [OK]
Common Mistakes:
  • Trying to assign store.value directly
  • Using update() with a value instead of a function
  • Using non-existent methods like change()
3. Given this Svelte code using Context API:
import { setContext, getContext } from 'svelte';

// Parent.svelte
setContext('color', 'blue');

// Child.svelte
const color = getContext('color');

What will color be inside Child.svelte?
medium
A. Throws an error
B. undefined
C. null
D. 'blue'

Solution

  1. Step 1: Understand setContext and getContext

    setContext('color', 'blue') sets a value accessible to child components via getContext('color').
  2. Step 2: Check child access

    Child.svelte calls getContext('color') and receives 'blue' because it is a child of the parent setting the context.
  3. Final Answer:

    'blue' -> Option D
  4. Quick Check:

    Context API passes 'blue' from parent to child [OK]
Hint: getContext returns parent's setContext value [OK]
Common Mistakes:
  • Expecting undefined if not wrapped properly
  • Confusing context keys or forgetting to setContext
  • Assuming context works globally like stores
4. What is wrong with this Svelte store usage?
import { writable } from 'svelte/store';

const count = writable(0);

function increment() {
  count = count + 1;
}
medium
A. writable stores cannot hold numbers.
B. You cannot reassign the store variable; must use count.update() or count.set().
C. The import statement is incorrect.
D. The function increment should be async.

Solution

  1. Step 1: Identify store usage error

    The code tries to do count = count + 1, which reassigns the store variable instead of updating its value.
  2. Step 2: Correct way to update store

    Use count.update(n => n + 1) or count.set(newValue) to change the store's value reactively.
  3. Final Answer:

    You cannot reassign the store variable; must use count.update() or count.set(). -> Option B
  4. Quick Check:

    Update store with update() or set(), not reassignment [OK]
Hint: Use update() or set(), never reassign store variable [OK]
Common Mistakes:
  • Trying to assign store variable directly
  • Ignoring reactive update methods
  • Thinking writable stores can't hold numbers
5. You want to share a theme color across your entire Svelte app and reactively update it from any component. Which approach is best and why?
hard
A. Use a writable store for the theme color because stores provide reactive global state accessible anywhere.
B. Use Context API because it automatically updates all components globally.
C. Pass the theme color as a prop to every component manually.
D. Use a local variable in each component to store the theme color.

Solution

  1. Step 1: Identify requirement for global reactive state

    The theme color must be shared app-wide and update reactively from any component.
  2. Step 2: Compare Context API and stores

    Context API only shares data between parent and children, not globally. Stores provide reactive global state accessible anywhere.
  3. Final Answer:

    Use a writable store for the theme color because stores provide reactive global state accessible anywhere. -> Option A
  4. Quick Check:

    Global reactive state needs stores, not Context API [OK]
Hint: Global reactive state? Use stores, not Context API [OK]
Common Mistakes:
  • Choosing Context API for global state
  • Passing props everywhere manually
  • Using local variables that don't sync
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard