0
0
Svelteframework~20 mins

Context vs stores decision 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 vs stores in Svelte?

Which scenario is best suited for using context instead of stores in Svelte?

APassing data deeply through a component tree without prop drilling, but only within a specific subtree.
BManaging asynchronous data fetching with automatic updates.
CStoring user preferences that need to persist across page reloads.
DSharing reactive data globally across unrelated components anywhere in the app.
Attempts:
2 left
💡 Hint

Think about the scope and lifetime of data when choosing between context and stores.

component_behavior
intermediate
2:00remaining
Effect of updating a store vs context on components

Consider a Svelte app where a store and a context both hold a count value. If the store's count changes, which components update? What about if the context's count changes?

AAll components update for both store and context changes regardless of location.
BAll components subscribed to the store update; only components within the context subtree update when context changes.
CNo components update automatically; manual refresh is needed.
DOnly components within the context subtree update for both store and context changes.
Attempts:
2 left
💡 Hint

Remember how reactivity works with stores and context in Svelte.

📝 Syntax
advanced
2:00remaining
Correct syntax to set and get context in Svelte

Which option shows the correct way to set and get context in Svelte components?

AIn parent: <code>setContext('key', value);</code> In child: <code>const val = getContext('key');</code>
BIn parent: <code>context.set('key', value);</code> In child: <code>const val = context.get('key');</code>
CIn parent: <code>getContext('key', value);</code> In child: <code>const val = setContext('key');</code>
DIn parent: <code>set('key', value);</code> In child: <code>const val = get('key');</code>
Attempts:
2 left
💡 Hint

Check official Svelte API for context functions.

🔧 Debug
advanced
2:00remaining
Why does a Svelte store update not reflect in a component?

A Svelte component subscribes to a writable store but does not update when the store value changes. What is the most likely cause?

AThe store value was updated with <code>store.set()</code> instead of <code>store.update()</code>.
BThe store was created with <code>readable</code> instead of <code>writable</code>.
CThe component uses <code>$store</code> syntax but forgot to declare <code>let $store</code>.
DThe component did not import the store instance correctly and is using a different store object.
Attempts:
2 left
💡 Hint

Check if the component and the store share the same instance.

state_output
expert
3:00remaining
Output after context and store updates in nested components

Given the following Svelte setup:

Parent.svelte:




Child.svelte:

Context count: {contextCount}

Store count: {storeCount}

What is the output after clicking the button twice?

A
Context count: 0
Store count: 2
B
Context count: 12
Store count: 2
C
Context count: 10
Store count: 2
D
Context count: 10
Store count: 0
Attempts:
2 left
💡 Hint

Remember context is static unless reset; store updates reactively change subscribed values.