Performance: Creating a Pinia store
MEDIUM IMPACT
This affects the initial load time and runtime responsiveness by managing state efficiently in Vue apps.
import { defineStore } from 'pinia'; export const useCounterStore = defineStore('counter', { state: () => ({ count: 0 }), actions: { increment() { this.count++; } } }); // Components import and use this store for shared reactive state
const count = ref(0); export default { setup() { return { count }; } }; // Each component imports and uses this ref directly without a store
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct reactive refs in multiple components | Multiple reactive refs duplicated | Multiple re-renders per update | Higher paint cost due to redundant updates | [X] Bad |
| Centralized Pinia store usage | Single reactive state shared | Minimal re-renders triggered | Lower paint cost with efficient updates | [OK] Good |