Performance: Creating a Pinia store
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 |
defineStore?const useCounterStore = defineStore('counter', {
state: () => ({ count: 0 }),
actions: {
increment() { this.count++ }
}
})count after calling increment() twice?const useTodoStore = defineStore('todo', {
state: () => ({ todos: [] }),
actions: {
addTodo(todo) {
this.todos.push(todo)
}
}
})
// Usage
const todoStore = useTodoStore()
todoStore.addTodo('Learn Pinia')Pass the test and this pattern moves unknown