Menu
CoddyTech

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 icon

Challenge

Medium

Compose 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>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Routing and State