Don't quite understand reactivity #1448
Replies: 3 comments 3 replies
-
You are indeed misunderstanding some of the documentation here. You have to remember that under the hood of Pinia is just an object declared using the The doc is really telling you that, just like any reactive object in vue, there are specific ways to access properties so that they remain reactive.
All of this applies to any object created with What
The same rules applies for state values and getter values. Once you extract the value in a non observable manner, you loose reactivity. The only difference with getters is that they are re-computed automatically when the state of the store changes. They are implemented as computed values under the hood, but they are still exposed on the reactive object in an unwrapped form. Meaning, in the getter doc that you linked, the type of
|
Beta Was this translation helpful? Give feedback.
-
I think it's worth reading https://vuejs.org/guide/essentials/reactivity-fundamentals.html#limitations-of-reactive if you don't understand why |
Beta Was this translation helpful? Give feedback.
-
This makes no sense whatsoever. If we define refs inside a store, e.g. via the setup api, why would we need to ask for refs again by calling It looks to me as if pinia is trying to be unnecessarily smart, causing unneccesary confusion by doing so. At no point in the docs it is stated that accessing store properties will lose reactivity https://pinia.vuejs.org/core-concepts/. Imo It would at least be nice to update it so it's clear the only correct way to keep reactivity is |
Beta Was this translation helpful? Give feedback.
-
I have some questions regarding Pinia and reactivity. I'm losing reactivity, possibly in multiple ways.
The docs here https://pinia.vuejs.org/core-concepts/#using-the-store say:
I sure think it would be more helpful if that sentence continued with something like "but doing so will lose reactivity.", if that's correct.
Then farther down the same page it says:
That makes it sound like we shouldn't access properties directly on the store as the first referenced documentation suggests, unless I am misunderstanding what 'properties' are.
When that says 'properties' does that apply to state properties only (the properties on the state itself) or does it also include getters and actions? I'm losing reactivity on (some, if not all) my getters.
But here https://pinia.vuejs.org/core-concepts/getters.html they say:
When I examine in devtools in my component's code, a getter is just an object but a local computed is a ComputedRefImpl. Is that an indication something is wrong with my getter thereby explaining why I'm losing reactivity? In the image below, dashboardDealStage is a local computed in the component and dealStore.getDashboardDeal is my getter (and it's returning an object, not a ComputedRefImpl).
After the documentation states this:
It demonstrates using storeToRefs to maintain reactivity. Doing this makes my getters reactive, meaning this works!
const { getDashboardDeal: dashboardDeal } = storeToRefs(dealStore);
But, that creates a ref. If getters are computeds, why should I need to create a ref for them? That doesn't make sense to me which makes me think I'm misunderstanding something and doing something wrong.
What am I missing? Thanks!
Beta Was this translation helpful? Give feedback.
All reactions