Consider a parent component that sets a writable store in context and a child component that subscribes to it. What will the child display?
/* 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>
Remember that setContext shares the store instance, and $count auto-subscribes.
The parent sets a writable store with initial value 5 in context. The child gets the same store and uses $count to auto-subscribe and display its value. So the output is 5.
$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?
/* 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>
The store updates asynchronously after 100ms. The child subscribes to the store.
The store initially has name 'Alice'. After 100ms, the store updates to name 'Bob'. The child component reacts to this update and displays 'Bob'.
Identify the correct code snippet that sets a readable store in context and retrieves it in a child component.
Check how the readable store's start function uses the set callback.
Option D correctly uses the set callback inside the readable store's start function to update the store value every second. The child subscribes and displays the time.
Option D incorrectly tries to call time.set which does not exist on a readable store.
Option D does not provide a start function, so the store never updates.
Option D calls set only once and does not update periodically.
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?
/* 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>
Check how Svelte auto-subscription works with stores in templates.
The child component uses {count} which prints the store object itself, not its value. To auto-subscribe and get the current value, it must use {$count}.
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')?
Think about how setContext works when called multiple times with the same key.
In Svelte, setContext stores a value by key. If called multiple times with the same key, the last value overwrites the previous one. So the child gets the last store set.