Pinia · State
PINIA0004Write store state through the action that already writes it
Report Pinia state that the store's own action writes, yet two or more other files write directly.
vite-doctor/piniastateinfoNo fix
Why it happens
Pinia lets any component write store state, but an action that already writes a property shows that the store author meant changes to go through it. When several components write the same property directly, the changes are scattered across files and skip whatever the action enforces, such as validation, derived bookkeeping, or persistence.
Fix
Call the store's existing action from the components, or add an action for a change it does not cover yet, such as a reset. Keep the property in the store's state; do not copy it into components.
Example
Change state through the store's action
Before
<!-- stores/cart.ts -->
export const useCartStore = defineStore('cart', () => {
const items = ref<string[]>([])
function addItem(item: string) {
items.value.push(item)
}
return { items, addItem }
})
<!-- AddButton.vue and ClearButton.vue -->
<script setup lang="ts">
const cart = useCartStore()
</script>
<template>
<button @click="cart.items.push('apple')">Add</button>
<button @click="cart.items = []">Clear</button>
</template>
After
<!-- stores/cart.ts -->
export const useCartStore = defineStore('cart', () => {
const items = ref<string[]>([])
function addItem(item: string) {
items.value.push(item)
}
function clear() {
items.value = []
}
return { items, addItem, clear }
})
<!-- AddButton.vue and ClearButton.vue -->
<script setup lang="ts">
const cart = useCartStore()
</script>
<template>
<button @click="cart.addItem('apple')">Add</button>
<button @click="cart.clear()">Clear</button>
</template>
Verify the fix
Run only this rule after editing so the report stays focused on the diagnostic you are closing:
pnpm vite-doctor . --extends auto,pinia/strict --rules pinia/state/no-action-bypass