Store-to-store interaction helps different parts of your app share and update data easily.
Store-to-store interaction in Vue
import { defineStore } from 'pinia' // Store A export const useStoreA = defineStore('storeA', { state: () => ({ count: 0 }), actions: { increment() { this.count++ } } }) // Store B import { useStoreA } from './storeA' export const useStoreB = defineStore('storeB', { actions: { incrementStoreA() { const storeA = useStoreA() storeA.increment() } } })
Use useStoreA() inside another store to access its state and actions.
This pattern keeps stores independent but able to communicate.
import { defineStore } from 'pinia' export const useStoreA = defineStore('storeA', { state: () => ({ message: 'Hello' }), actions: { updateMessage(newMsg) { this.message = newMsg } } })
import { defineStore } from 'pinia' import { useStoreA } from './storeA' export const useStoreB = defineStore('storeB', { actions: { changeMessageInStoreA() { const storeA = useStoreA() storeA.updateMessage('Hi from Store B') } } })
This example shows Store B calling Store A's increment action. Clicking the button increases Store A's count.
import { createApp } from 'vue' import { createPinia, defineStore } from 'pinia' // Store A export const useStoreA = defineStore('storeA', { state: () => ({ count: 0 }), actions: { increment() { this.count++ } } }) // Store B import { useStoreA } from './storeA' export const useStoreB = defineStore('storeB', { actions: { incrementStoreA() { const storeA = useStoreA() storeA.increment() } } }) // Vue component const App = { template: ` <div> <p>Store A count: {{ storeA.count }}</p> <button @click="storeB.incrementStoreA">Increment Store A from Store B</button> </div> `, setup() { const storeA = useStoreA() const storeB = useStoreB() return { storeA, storeB } } } const app = createApp(App) const pinia = createPinia() app.use(pinia) app.mount('#app')
Always import the store you want to interact with inside another store.
Calling one store's action from another keeps your code clean and modular.
Make sure to use Pinia's useStore() functions inside setup or actions, not outside component setup.
Store-to-store interaction lets stores share and update data.
Use one store's useStore() inside another to call its actions or access state.
This helps keep your app organized and your data consistent.
Practice
Solution
Step 1: Understand store-to-store interaction
Store-to-store interaction means one store can use another store's state or actions to share data or trigger updates.Step 2: Identify the purpose
This interaction helps keep data consistent by letting stores communicate, not by replacing components or syncing automatically.Final Answer:
To allow one store to access or call actions from another store -> Option BQuick Check:
Store-to-store interaction = Access other store's actions/state [OK]
- Thinking stores replace components
- Assuming stores sync automatically with backend
- Believing stores are always independent
Solution
Step 1: Import the store function before usage
You must import the store function (e.g., useStoreA) before calling it to get the store instance.Step 2: Call the store functions to get instances
Call useStoreA() and useStoreB() to get their store instances for interaction.Final Answer:
import { useStoreA } from './storeA'; const storeB = useStoreB(); const storeA = useStoreA(); -> Option DQuick Check:
Import then call stores = correct syntax [OK]
- Calling useStore before importing
- Mixing store names when calling
- Incorrect import syntax without braces
storeB.incrementA()?
const useStoreA = defineStore('A', {
state: () => ({ count: 0 }),
actions: { increment() { this.count++ } }
});
const useStoreB = defineStore('B', {
actions: {
incrementA() {
const storeA = useStoreA();
storeA.increment();
console.log(storeA.count);
}
}
});
const storeB = useStoreB();
storeB.incrementA();Solution
Step 1: Understand storeA initial state and increment
storeA starts with count = 0. Calling increment() increases count by 1.Step 2: Trace incrementA action in storeB
incrementA calls storeA.increment(), then logs storeA.count which is now 1.Final Answer:
1 -> Option AQuick Check:
Increment count from 0 to 1, then log 1 [OK]
- Assuming count stays 0 after increment
- Thinking console logs undefined
- Forgetting to call useStoreA inside action
const useStoreA = defineStore('A', {
state: () => ({ value: 10 })
});
const useStoreB = defineStore('B', {
actions: {
updateValue() {
const storeA = useStoreA;
storeA.value = 20;
}
}
});Solution
Step 1: Check how useStoreA is used inside updateValue
useStoreA is referenced without parentheses, so storeA is a function reference, not a store instance.Step 2: Understand consequences of missing parentheses
Without calling useStoreA(), you cannot access or modify state like storeA.value.Final Answer:
useStoreA is not called as a function, so storeA is not a store instance -> Option CQuick Check:
Call useStoreA() to get store instance [OK]
- Forgetting parentheses when calling useStore
- Trying to assign state directly without actions
- Misunderstanding defineStore usage
useCartStore managing cart items and useUserStore managing user info. You want to clear the cart when the user logs out. How should you implement this store-to-store interaction inside useUserStore?Solution
Step 1: Identify the correct way to trigger cart clearing
Calling useCartStore() inside useUserStore's logout action gets the cart store instance.Step 2: Use cart store's action to clear items
Calling cart.clearItems() properly updates the cart state in a controlled way.Final Answer:
Inside the logout action, call const cart = useCartStore(); cart.clearItems(); -> Option AQuick Check:
Call other store's action inside logout to clear cart [OK]
- Trying to modify other store's state directly
- Using events instead of direct store calls
- Expecting automatic reset without explicit calls
Ready to prove it?
Pass the test and this pattern moves unknown
