Composing Related Stores
Part of the Routing and State section of Coddy's Vue journey. Lesson 39 of 55.
A store can use another store to derive a result or coordinate an action. Keep the dependency explicit and read the other store reactively instead of copying its initial primitive value. This avoids duplicating a shared setting in multiple stores.
const rates=useRatesStore();
const units=ref(2);
const cost=computed(()=>units.value*rates.unitPrice);Prefer a clear dependency direction. Two setup stores should not immediately read each other during construction, because circular initialization can fail. Shared reads inside computed getters or actions can be appropriate, but design the ownership so each source value has one home.
Challenge
MediumCompose the plan and rate stores. The rate store starts at price 3; the plan starts with 1 seat. Its computed total reads seats * rates.price from the live rate store. Keep Add seat and Raise rate independent and show the derived total in p#total. Do not copy the initial rate into a separate snapshot.
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 Filters2Navigation 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