Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A Vue component does not re-render when the Pinia store changes

ErrorThe store value changes in Vue devtools but the template still shows the old value

Short answer

The store itself is reactive, but copies of its values are not. If you destructure state out of the store, replace $state, or add a property that was never declared in state, Vue has nothing to track. Read through the store or storeToRefs(), change state with $patch, and declare every property up front.

  • Applies to Vue 3, Pinia 3, Vite 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the store property and the local variable your template uses, next to each other, after you trigger the change. If the store is right and the variable is stale, you copied the value.

    const store = useCounterStore()
    console.log(store.count, count)   // 'count' is the variable the template shows
  2. 2Match what you see, then jump to its fix

    1. 1const { count, name } = useCounterStore() in script setup, and the values never changeState was destructured out of the storeFix
    2. 2Code such as store.$state = newData, then the view does not followThe whole state object was replacedFix
    3. 3store.secondCount = 2 works in the console but no component ever reactsA property was never declared in stateFix

What does the error look like?

  • The store value changes in Vue devtools but the template still shows the old value
  • The component updates only after a full page reload

Cause 1 of 3

State was destructured out of the store

1Check

Pinia's docs say a store is a reactive object, so destructuring it breaks reactivity: you get plain values, not live ones.

grep -rn --include=*.vue --include=*.ts -E "const \{[^}]+\} = use[A-Za-z]+Store\(" src/

2Fix

Keep the store object and read from it, or use storeToRefs() for state and getters. Actions can be destructured directly because they are bound to the store.

import { storeToRefs } from 'pinia'

const store = useCounterStore()
const { count, doubleCount } = storeToRefs(store)   // stays reactive
const { increment } = store                          // actions are safe

3Verify

Trigger the action in the browser. The template now follows the store.

npm run dev

Confirmed by pinia.vuejs.org

Did this fix it?

Cause 2 of 3

The whole state object was replaced

1Check

The state page says you cannot replace the state of a store, because that would break reactivity.

grep -rn --include=*.vue --include=*.ts '\$state *=' src/

2Fix

Update the existing state with $patch, which takes a partial object or a function.

store.$patch({ user: newUser, items: newItems })
// or for arrays:
store.$patch((state) => {
  state.items.push(newItem)
})

3Verify

Run the same action again and watch the component update.

npm run dev

Confirmed by pinia.vuejs.org

Did this fix it?

Cause 3 of 3

A property was never declared in state

1Check

Pinia's docs say properties missing from state() cannot be added later, and every piece of state must be declared, even with an initial value of undefined.

sed -n '1,40p' src/stores/counter.ts

2Fix

Add the property to state with an initial value, then assign to it.

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0,
    secondCount: undefined as number | undefined,
  }),
})

3Verify

Assign to the property again and check that the template updates.

npm run dev

Confirmed by pinia.vuejs.org

Did this fix it?

How do I stop it coming back?

  • Use the store as store.x in templates and scripts, and reach for storeToRefs() when you want a short name.
  • Declare every state property in the initial state, even if the value starts as undefined.
  • Change state with actions or $patch instead of replacing objects the store owns.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Web apps

A POST to a Google Apps Script web app returns 405 or a CORS error

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.