Pinia · PINIA0004
Write 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.
Run this rule
pnpm vite-doctor . --extends auto,pinia/strict --rules pinia/state/no-action-bypass
Why it matters
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.
Recommended 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.
Useful links
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>