0
0
Vueframework~10 mins

Persisting store state in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Persisting store state
App starts
Load store state from storage?
NoUse default state
Yes
Initialize store with loaded state
User interacts -> store state changes
Save updated state to storage
Back to User interacts
When the app starts, it tries to load saved state from storage. If found, it initializes the store with it. When the user changes state, the new state is saved back to storage.
Execution Sample
Vue
import { reactive, watch } from 'vue'
const store = reactive({ count: 0 })
const saved = localStorage.getItem('store')
if (saved) store.count = JSON.parse(saved).count
watch(store, () => localStorage.setItem('store', JSON.stringify(store)))
This code loads a saved count from localStorage into a reactive store and saves changes back automatically.
Execution Table
StepActionStore State BeforeOperationStore State AfterStorage Interaction
1App starts{ count: 0 }Check localStorage{ count: 0 }localStorage.getItem('store') returns '{"count":5}'
2Load saved state{ count: 0 }Parse and assign{ count: 5 }No change
3User clicks increment{ count: 5 }store.count++{ count: 6 }No change yet
4Watch triggers{ count: 6 }Save to localStorage{ count: 6 }localStorage.setItem('store', '{"count":6}')
5User clicks increment again{ count: 6 }store.count++{ count: 7 }No change yet
6Watch triggers{ count: 7 }Save to localStorage{ count: 7 }localStorage.setItem('store', '{"count":7}')
7App reloads{ count: 0 }Check localStorage{ count: 0 }localStorage.getItem('store') returns '{"count":7}'
8Load saved state{ count: 0 }Parse and assign{ count: 7 }No change
9Exit{ count: 7 }No more actions{ count: 7 }State persisted
💡 Execution stops because no more user actions occur and state is persisted.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
store.count056777
localStorage 'store'{"count":5}{"count":5}{"count":6}{"count":7}{"count":7}{"count":7}
Key Moments - 3 Insights
Why does the store state update after loading from localStorage?
Because at step 2, the saved JSON string is parsed and assigned to the reactive store, updating store.count from 0 to 5 as shown in the execution_table.
When does the localStorage get updated with the new store state?
At steps 4 and 6, the watch function triggers after store.count changes, saving the updated state to localStorage as shown in the Storage Interaction column.
What happens if localStorage is empty when the app starts?
At step 1, localStorage.getItem returns null, so the store keeps its default state (count: 0) as shown in the execution_table row 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is store.count after step 4?
A6
B5
C7
D0
💡 Hint
Check the 'Store State After' column at step 4 in the execution_table.
At which step does localStorage first get updated with the new store state?
AStep 6
BStep 2
CStep 4
DStep 8
💡 Hint
Look at the 'Storage Interaction' column to see when localStorage.setItem is first called.
If localStorage was empty at app start, what would store.count be after step 2?
Anull
B0
C5
Dundefined
💡 Hint
Refer to the 'Store State After' column at step 2 and the explanation in key_moments about empty localStorage.
Concept Snapshot
Persisting store state in Vue:
- On app start, load saved state from localStorage if exists.
- Initialize reactive store with loaded or default state.
- Use watch to save store changes back to localStorage.
- This keeps state across page reloads.
- localStorage stores strings, so use JSON.stringify/parse.
Full Transcript
This visual execution shows how a Vue reactive store can keep its state saved in localStorage. When the app starts, it checks localStorage for saved data. If found, it loads and parses it to update the store's state. When the user changes the store (like incrementing a count), a watch function triggers and saves the new state back to localStorage as a JSON string. This way, the state persists even if the page reloads. The execution table traces each step, showing store values and storage interactions. The variable tracker highlights how store.count and localStorage content change over time. Key moments clarify common confusions like when loading and saving happen. The quiz tests understanding by asking about specific steps and values. This method helps beginners see exactly how persisting store state works in Vue with localStorage.