Store-to-store interaction
📖 Scenario: You are building a small Vue 3 app with two separate stores. One store manages a list of products, and the other store manages a shopping cart. You want the cart store to react to changes in the product store, such as updating product availability.
🎯 Goal: Create two Pinia stores: useProductStore and useCartStore. The useCartStore should watch the products in useProductStore and update the cart items accordingly.
📋 What You'll Learn
Create a Pinia store called
useProductStore with a products array containing objects with id, name, and available properties.Create a Pinia store called
useCartStore with a cartItems array containing objects with productId and quantity.In
useCartStore, add a watcher that reacts to changes in products from useProductStore and removes cart items for products that are no longer available.Use Vue 3 Composition API and Pinia 2+ syntax with
defineStore and watch.💡 Why This Matters
🌍 Real World
Many real-world Vue apps use multiple stores to separate concerns, such as products and cart. Keeping stores in sync is essential for a smooth user experience.
💼 Career
Understanding store-to-store interaction is important for frontend developers working with Vue and Pinia in e-commerce or complex state management scenarios.
Progress0 / 4 steps