Performance: Why state management is needed
MEDIUM IMPACT
State management affects how efficiently a Vue app updates the UI and handles user interactions, impacting rendering speed and responsiveness.
import { createPinia, defineStore } from 'pinia'; const useCounterStore = defineStore('counter', { state: () => ({ count: 0 }), actions: { increment() { this.count++ } } }); // Components use useCounterStore() to access and update state centrally
const sharedData = reactive({ count: 0 }); // Each component imports and modifies sharedData directly without a central store
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct shared reactive data without store | Many components update DOM nodes | Multiple reflows triggered | High paint cost due to redundant updates | [X] Bad |
| Centralized state management with Pinia | Only affected components update DOM | Minimal reflows | Lower paint cost with targeted updates | [OK] Good |