0
0
Svelteframework~10 mins

Context API vs stores in Svelte - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Context API vs stores
Create Store
Subscribe Components
Update Store Value
All Subscribers React
Create Context
Provide Context Value
Consume Context in Descendants
Descendants React to Changes
Shows how stores hold shared state and notify subscribers, while Context API passes data down the component tree for descendants to consume.
Execution Sample
Svelte
// Store example
import { writable } from 'svelte/store';
const count = writable(0);

// Context example
import { setContext, getContext } from 'svelte';
setContext('key', 'value');
const val = getContext('key');
This code creates a store with a number and sets a context value, showing two ways to share data in Svelte.
Execution Table
StepActionStore ValueContext ValueEffect on Components
1Create store with initial value 00undefinedNo components subscribed yet
2Subscribe component A to store0undefinedComponent A will react to store changes
3Update store value to 11undefinedComponent A re-renders with new value 1
4Set context key 'key' to 'value'1'value'Descendant components can access context
5Component B calls getContext('key')1'value'Component B receives 'value' from context
6Update store value to 22'value'Component A re-renders with new value 2; context unchanged
7No context update2'value'Context consumers see same value unless context changes
8End of trace2'value'Store and context values stable
💡 Execution stops after store and context values stabilize with subscribers updated.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
store (count)000122
context ('key')undefinedundefinedundefinedundefined'value''value'
Key Moments - 2 Insights
Why does updating the store value cause components to re-render but changing context requires explicit setContext?
Stores are reactive and notify subscribers automatically on update (see steps 3 and 6 in execution_table). Context values must be set explicitly with setContext (step 4) and only descendants that call getContext receive the value.
Can components outside the context provider access the context value?
No, only components inside the provider tree that call getContext with the matching key receive the context value (step 5). Stores can be imported anywhere and subscribed to globally.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the store value at step 3?
Aundefined
B0
C1
D2
💡 Hint
Check the 'Store Value' column at step 3 in the execution_table.
At which step does the context value get set?
AStep 4
BStep 2
CStep 6
DStep 1
💡 Hint
Look for 'Set context key' action in the execution_table.
If the store value was never updated, what would happen to component A's rendering?
AIt would re-render only when context changes
BIt would never re-render after subscription
CIt would re-render every step
DIt would throw an error
💡 Hint
Refer to steps 2 and 3 in execution_table and variable_tracker for store updates.
Concept Snapshot
Svelte stores hold reactive shared state and notify all subscribers on change.
Context API passes data down the component tree for descendants to consume.
Stores can be imported and subscribed anywhere; context is scoped to descendants.
Update stores with set() or update(); setContext and getContext manage context.
Use stores for global reactive state; use context for passing data deeply without props.
Full Transcript
This visual trace compares Svelte's Context API and stores for sharing data. Stores are created with writable() and hold reactive values. Components subscribe to stores and re-render automatically when the store updates. Context API uses setContext to provide a value to descendant components, which access it with getContext. The execution table shows steps creating a store, subscribing components, updating values, setting context, and consuming context. Variable tracking shows store and context values changing over time. Key moments clarify that stores notify subscribers automatically, while context requires explicit provision and is scoped to descendants. The quiz tests understanding of store updates, context setting, and component re-rendering behavior. The snapshot summarizes when to use stores versus context in Svelte.