State not reactive when updating from external function
See original GitHub issueMaybe 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:
- Created 4 years ago
- Reactions:3
- Comments:6 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for that insight. On second thought, that makes total sense 👍
Moving forward, I’ll be using
which seems to be working as expected 🙂
Something important to notice, even using
storeToRefs
I was facing same issue, the problem was the spread operator I was using to separate functionsWrong way:
Correct way: