State
state/pinia-store-to-refs
State and getters taken out of a Pinia store go through storeToRefs(); plain destructuring freezes them at their first value.
seriousstaticfe-state
Why
A Pinia store is a reactive object. const { name, total } = useCartStore() copies the current values into plain variables, so the template keeps showing them after the store changes: the cart badge says 0 while the cart fills up. Nothing warns about it.
Fix
Use const { name, total } = storeToRefs(store) for state and getters, or read them through the store (cart.total). Actions are bound to the store and may be destructured directly: const { addItem } = cart.
Examples
// bad
const { items, total, addItem } = useCartStore()
// good
const cart = useCartStore()
const { items, total } = storeToRefs(cart)
const { addItem } = cartSources
Facts
- Id
state/pinia-store-to-refs- Severity
- serious
- Check
- static checked in the source by
audit - Applies to
vue- Only in projects with
pinia- Taught by the skill
fe-stateState and stores- Since version
- 0.1.0
Read it in the terminal
npx github:dawidolko/frontend-skills explain state/pinia-store-to-refs