0
0
Svelteframework~20 mins

Why stores manage shared state in Svelte - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Store Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when multiple components subscribe to the same Svelte store?

Consider a Svelte store shared by two components. When the store value changes, what is the expected behavior?

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

const count = writable(0);

// Component A and Component B both subscribe to 'count'
// When count.set(5) is called, what happens?
AOnly the component that called count.set(5) updates; the other stays the same.
BBoth components update their displayed value to 5 automatically.
CNeither component updates until the page is refreshed.
DComponents update only if they manually call count.subscribe() again.
Attempts:
2 left
💡 Hint

Think about how reactive subscriptions work in Svelte stores.

🧠 Conceptual
intermediate
2:00remaining
Why use a store for shared state instead of component props?

Why is a Svelte store preferred for managing state shared across many components instead of passing props down multiple levels?

AProps automatically sync data between components without extra code.
BProps are faster and more efficient for sharing state across many components.
CStores allow direct access to shared data without prop drilling, making code cleaner and easier to maintain.
DStores require components to reload to see updated data, unlike props.
Attempts:
2 left
💡 Hint

Think about how data flows in component trees and how stores simplify that.

📝 Syntax
advanced
2:30remaining
Which Svelte store syntax correctly creates a readable store with a custom start function?

Choose the correct syntax to create a readable store that starts a timer and updates every second.

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

Remember the readable store's start function must return a cleanup function.

🔧 Debug
advanced
2:30remaining
Why does this Svelte store subscription not update the component?

Given this code, why does the component not update when the store changes?

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

// In component:
let value;
count.subscribe(val => {
  value = val;
});

// count.set(1) is called elsewhere
AThe component does not react because 'value' is not a reactive variable; it needs '$value' or '$count' syntax.
BThe subscription callback is never called because count.set(1) is invalid.
CThe component updates only if 'value' is declared with 'const' instead of 'let'.
DThe store must be imported as 'readable' to update the component.
Attempts:
2 left
💡 Hint

Think about how Svelte tracks reactive variables and updates the UI.

state_output
expert
3:00remaining
What is the final output after these Svelte store updates?

Given the following Svelte store and component code, what is the final value displayed?

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

const store = writable(0);

store.update(n => n + 1);
store.set(5);
store.update(n => n * 2);

let value;
store.subscribe(v => value = v);

// What is the value of 'value' after these operations?
A10
B6
C1
D5
Attempts:
2 left
💡 Hint

Trace each store operation step by step.