question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

State not reactive when updating from external function

See original GitHub issue

Maybe I’m using it wrong, but I thought something like this should work:

// component.vue

import { defineComponent } from '@vue/composition-api';

export default defineComponent({
  setup() {
    const app = useAppStore();

    const loading = app.state.loading;

    return { loading };
  })
});

// external function

function startLoading() {
    const app = useAppStore();
    app.state.loading = true;
}

Whenever this external function is called, my component doesn’t update based on the state change. Only if I wrap the state in toRefs does it update correctly when being changed “from the outside”:

import { defineComponent, toRefs } from '@vue/composition-api';

export default defineComponent({
  setup() {
    const app = useAppStore();

    // now it updates correctly when startLoading() is called somewhere else
    const loading = toRefs(app.state).loading;
    return { loading };
  })
});

Am I misunderstanding something here, or is this a bug?

I made a CodeSandbox with an example: https://codesandbox.io/s/pinia-example-rbb44

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

19reactions
rijkvanzantencommented, Mar 17, 2020

Thanks for that insight. On second thought, that makes total sense 👍

Moving forward, I’ll be using

const { loading } = toRefs(app.state);

which seems to be working as expected 🙂

7reactions
hjJuniorcommented, May 7, 2022

Something important to notice, even using storeToRefs I was facing same issue, the problem was the spread operator I was using to separate functions

Wrong way:

    const { login, ...store }= useAuthStore();
    const { user, isAuthenticated } = storeToRefs(store);

Correct way:

    const store = useAuthStore();
    const { user, isAuthenticated } = storeToRefs(store);
    const { login } = store;
Read more comments on GitHub >

github_iconTop Results From Across the Web

State not reactive when updating from external function #106
Whenever this external function is called, my component doesn't update based on the state change. Only if I wrap the state in toRefs...
Read more >
Why Your Vue Component Isn't Updating (and how to fix it)
1. Check that variables are reactive · You're updating a variable somewhere in your component, but your component isn't updating · You are...
Read more >
vue component doesn't update after state changes in pinia store
The Problem is, that when the state gets updated, it doesn't get updated in the components and the components themselves don't update either....
Read more >
Reactivity in Depth - Vue.js
A shallow ref is only reactive when its .value property is accessed - the inner value is left intact. When the external state...
Read more >
Updating Reactive Values Can Cause Some Non ... - Ben Nadel
js document clearly states, the Provide and Inject features don't support reactivity by design. As such, seeing the injected value changes get " ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found