0
0
Svelteframework~10 mins

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

Choose your learning style9 modes available
Concept Flow - Context vs stores decision
Start
Need to share data?
Yes
Is data local to component tree?
NoUse store
Yes
Use context API
Components access data
Update data triggers UI refresh
End
Decide if data is shared locally in a component tree (use context) or globally across app (use store). Components then access and update data accordingly.
Execution Sample
Svelte
import { writable } from 'svelte/store';
import { setContext, getContext } from 'svelte';

// Store example
const count = writable(0);

// Context example
setContext('key', count);
Shows creating a store and setting it in context for child components to access.
Execution Table
StepActionData LocationMethod UsedResult
1Decide to share dataLocal treeUse context APIsetContext called with key and data
2Child component accesses dataLocal treegetContext with keyReceives data from context
3Update data in contextLocal treeUpdate store inside contextUI updates in components using context
4Decide to share dataGlobal appUse store directlyStore created with writable()
5Any component subscribesGlobal appSubscribe to storeReceives current data and updates
6Update store valueGlobal appCall store.set()All subscribers update UI
7End--Data sharing and updates complete
💡 Data sharing method chosen based on scope: context for local tree, store for global app
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 6Final
countundefinedwritable(0) in contextupdated value (e.g., 1)writable(0) global storeupdated value (e.g., 2)final updated value
Key Moments - 3 Insights
Why use context instead of a store for local data?
Context shares data only within a specific component subtree, avoiding global subscriptions. See execution_table step 1 and 2 where context is set and accessed locally.
Can stores be used inside context?
Yes, stores can be passed via context to share reactive data locally. Execution_table step 3 shows updating a store inside context triggers UI updates.
When should I choose a store over context?
Use stores when data must be shared globally across many unrelated components. Execution_table steps 4-6 show global store creation and updates.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is context data accessed by a child component?
AStep 4
BStep 2
CStep 5
DStep 6
💡 Hint
Check the 'Method Used' column for 'getContext' usage in execution_table step 2
According to variable_tracker, what is the value of 'count' after step 3?
Aupdated value (e.g., 1)
Bwritable(0)
Cundefined
Dfinal updated value
💡 Hint
Look at the 'After Step 3' column for 'count' in variable_tracker
If data is shared globally, which method is used according to execution_table?
AsetContext
BgetContext
Cwritable store
Dlocal variable
💡 Hint
See execution_table steps 4-6 describing global data sharing
Concept Snapshot
Context vs Stores Decision in Svelte:
- Use context API to share data locally within a component tree.
- Use writable stores to share data globally across the app.
- Context passes data down without global subscriptions.
- Stores provide reactive global state accessible anywhere.
- Choose based on data scope and update needs.
Full Transcript
This visual execution shows how to decide between using context or stores in Svelte for sharing data. First, if data is only needed within a component subtree, use the context API by setting data with setContext and accessing it with getContext. This keeps data local and avoids global subscriptions. If data must be shared globally across many components, create a writable store and subscribe to it wherever needed. Updates to the store automatically refresh all subscribers. The execution table traces steps for both approaches, showing when context is set and accessed, and when stores are created and updated. The variable tracker follows the store variable 'count' as it changes. Key moments clarify why and when to use each method. The quiz tests understanding of these steps. This helps beginners visually grasp the decision and flow of data sharing in Svelte.