Extracting Reactive Fields
Part of the Routing and State section of Coddy's Vue journey. Lesson 34 of 55.
A store is reactive, but destructuring primitive state directly produces a snapshot. storeToRefs extracts refs for state and getters so their connection survives destructuring. Actions can be extracted directly because they remain bound to the store.
const basket=useBasketStore();
const {count,cost}=storeToRefs(basket);
const {add}=basket;
// In script: count.value
// In template: {{ count }}Use .value when reading an extracted ref in script, while templates unwrap it. Keep actions out of storeToRefs: that helper is for reactive state and getters. This lets a component expose a small set of local names without freezing the values at setup.
Challenge
MediumUse storeToRefs(store) to extract live boxes and total refs for the editor template. Extract actions add and remove directly from the store. The editor and Summary must continue to agree after repeated additions and removals.
Keep the supplied route readout and bootstrap files.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Routing practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Routing and State
4Query State
Reading Query ValuesCommitting Search QueriesPreserving Other FiltersNormalizing Query NumbersRecap: Catalog Filters7Pinia Options Stores
Shared Store StateDerived Store GettersStore Actions and BoundsExtracting Reactive FieldsRecap: Seat Counter2Navigation Actions
Navigation from HandlersReplacing HistoryHistory TravelCurrent Navigation LinksRecap: Save and Review5Nested Layouts
Parent and Child OutletsDefault Child ViewsRelative Child PathsAncestor Navigation StateRecap: Library Workspace8Store Design
Setup Store StructurePatching Related StateResetting Store StateComposing Related StoresRecap: Trip Pack11Workshop Planner Project
Planner RoutesShared Reservations