0
0
Svelteframework~20 mins

Context with stores in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Context Store Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Svelte component using context with a store?

Consider a parent component that sets a writable store in context and a child component that subscribes to it. What will the child display?

Svelte
/* Parent.svelte */
<script>
  import { writable } from 'svelte/store';
  import { setContext } from 'svelte';
  import Child from './Child.svelte';

  const count = writable(5);
  setContext('countStore', count);
</script>

<Child />

/* Child.svelte */
<script>
  import { getContext } from 'svelte';
  const count = getContext('countStore');
</script>

<p>{$count}</p>
A0
Bundefined
C5
DError: Cannot read property '$count' of undefined
Attempts:
2 left
💡 Hint

Remember that setContext shares the store instance, and $count auto-subscribes.

state_output
intermediate
2:00remaining
What is the value of $user.name after updating the store in context?

A Svelte app sets a store with an object in context. After updating the store's name property, what does the child component see?

Svelte
/* Parent.svelte */
<script>
  import { writable } from 'svelte/store';
  import { setContext } from 'svelte';

  const user = writable({ name: 'Alice', age: 30 });
  setContext('userStore', user);

  // Update name after 100ms
  setTimeout(() => {
    user.update(u => ({ ...u, name: 'Bob' }));
  }, 100);
</script>

<Child />

/* Child.svelte */
<script>
  import { getContext } from 'svelte';
  const user = getContext('userStore');
</script>

<p>{$user.name}</p>
ABob
BAlice
Cundefined
DError: Cannot read property 'name' of undefined
Attempts:
2 left
💡 Hint

The store updates asynchronously after 100ms. The child subscribes to the store.

📝 Syntax
advanced
2:30remaining
Which option correctly sets and gets a readable store in Svelte context?

Identify the correct code snippet that sets a readable store in context and retrieves it in a child component.

A
/* Parent.svelte */
import { readable } from 'svelte/store';
import { setContext } from 'svelte';
const time = readable(new Date(), set =&gt; {
  set(new Date());
  return () =&gt; {};
});
setContext('timeStore', time);

/* Child.svelte */
import { getContext } from 'svelte';
const time = getContext('timeStore');
&lt;p&gt;{$time.toLocaleTimeString()}&lt;/p&gt;
B
/* Parent.svelte */
import { readable } from 'svelte/store';
import { setContext } from 'svelte';
const time = readable(new Date(), () =&gt; {
  const interval = setInterval(() =&gt; time.set(new Date()), 1000);
  return () =&gt; clearInterval(interval);
});
setContext('timeStore', time);

/* Child.svelte */
import { getContext } from 'svelte';
const time = getContext('timeStore');
&lt;p&gt;{$time.toLocaleTimeString()}&lt;/p&gt;
C
/* Parent.svelte */
import { readable } from 'svelte/store';
import { setContext } from 'svelte';
const time = readable(new Date());
setContext('timeStore', time);

/* Child.svelte */
import { getContext } from 'svelte';
const time = getContext('timeStore');
&lt;p&gt;{$time.toLocaleTimeString()}&lt;/p&gt;
D
/* Parent.svelte */
import { readable } from 'svelte/store';
import { setContext } from 'svelte';
const time = readable(new Date(), set =&gt; {
  const interval = setInterval(() =&gt; set(new Date()), 1000);
  return () =&gt; clearInterval(interval);
});
setContext('timeStore', time);

/* Child.svelte */
import { getContext } from 'svelte';
const time = getContext('timeStore');
&lt;p&gt;{$time.toLocaleTimeString()}&lt;/p&gt;
Attempts:
2 left
💡 Hint

Check how the readable store's start function uses the set callback.

🔧 Debug
advanced
2:00remaining
Why does this Svelte child component fail to update when the store in context changes?

The parent sets a writable store in context and updates it. The child gets the store but does not update its display. What is the cause?

Svelte
/* Parent.svelte */
<script>
  import { writable } from 'svelte/store';
  import { setContext } from 'svelte';

  const count = writable(0);
  setContext('countStore', count);

  setTimeout(() => count.set(10), 100);
</script>

<Child />

/* Child.svelte */
<script>
  import { getContext } from 'svelte';
  const count = getContext('countStore');
</script>

<p>{count}</p>
AThe child uses {count} instead of {$count}, so it does not subscribe to the store.
BThe parent did not call setContext correctly, so the child gets undefined.
CThe store is not writable, so updates do not propagate.
DThe child must import the store directly instead of using getContext.
Attempts:
2 left
💡 Hint

Check how Svelte auto-subscription works with stores in templates.

🧠 Conceptual
expert
2:30remaining
What happens if you set a store in Svelte context and then overwrite it with another store under the same key?

In a Svelte app, a parent sets a writable store in context with key 'data'. Later, it sets a different readable store with the same key 'data'. What will a child component get when calling getContext('data')?

AThe child gets undefined because overwriting context keys is not allowed.
BThe child gets the second store (the readable one) because the last setContext call overwrites the previous value.
CThe child gets an array containing both stores.
DThe child gets the first store (the writable one) because setContext only sets once per key.
Attempts:
2 left
💡 Hint

Think about how setContext works when called multiple times with the same key.