Performance: Store-to-store interaction
This affects how quickly and efficiently state changes propagate between different stores, impacting UI responsiveness and rendering speed.
import { useStoreA } from './storeA'; import { useStoreB } from './storeB'; import { watch } from 'vue'; const storeA = useStoreA(); const storeB = useStoreB(); watch(() => storeA.sharedState, (newVal) => { storeB.sharedState = newVal; });
import { useStoreA } from './storeA'; import { useStoreB } from './storeB'; const storeA = useStoreA(); const storeB = useStoreB(); storeA.$subscribe(() => { storeB.sharedState = storeA.sharedState; });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct store subscription | Multiple reactive updates | Multiple reflows per update | High paint cost due to layout thrashing | [X] Bad |
| Using Vue watch for syncing stores | Single reactive update batch | Single reflow per update cycle | Lower paint cost with batched layout | [OK] Good |
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();const useStoreA = defineStore('A', {
state: () => ({ value: 10 })
});
const useStoreB = defineStore('B', {
actions: {
updateValue() {
const storeA = useStoreA;
storeA.value = 20;
}
}
});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?Pass the test and this pattern moves unknown