Non-reactive state #810
-
Is there a way to keep non-reactive state in a pinia store, when using either Vue 2 or 3? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Yes, you should be able to use Here is an example, note that adding items to the cart does not show reactively because the |
Beta Was this translation helpful? Give feedback.
-
Is it possible to do this when using Nuxt? Because it serializes the state server-side and it seems some of the concepts get lost when it initializes again client-side. I've had this issue when using Object.freeze on parts of the state |
Beta Was this translation helpful? Give feedback.
-
I'm working on an application that has a lot of data, most of which never changes, after being fetched from an API. With a lot of data and pages with many components, performance is becoming an issue and when using the Chrome Profiler, it shows that Vue is doing vast amounts of work during hydration, so I'm exploring ways of reducing the overhead. The act of converting a large object to a reactive one probably takes a lot of CPU, but in my case, its often not necessary, because the values can never change after the initial rendering. The state is composed from objects, some of which have a lot of properties. Mutations are often not necessary, however, we reached for Vuex, because the state must be accessible in many components that do not have a parent-child relationships. I'm now reaching for Pinia because of the simplified interface and I would really be glad if I could address this issue as well. I looked at the hydrate function, but it seems like I would need to modify the Nuxt serializer as well and add some sort of metadata to the object, if its an instance of Should I file an issue in the Nuxt repository, so that they have a look at it? In my opinion, if I add a reactive property via On a second thought, it would probably add some overhead to figure it out on the client. About the boiled down version, here's a short example
A list of products is fetched, lets say 60 of them. A product has multiple images, variants, discounts, labels, price variations etc. Sometimes, the products themselves are part of a larger object. For example, there can be a Featured Products module, which contains pre-configured theming options and is coming from the database with the products attached to it. I would like to be able to do something to the lines of
Note: I am aware that state management libraries are not created to specifically address the encapsulation of logic for fetching data, ie this would not be an issue if I was just fetching the products in the component, using a composable or something, but I'm hoping that you can spot the problem, despite the poor example |
Beta Was this translation helpful? Give feedback.
Yes, you should be able to use
markRaw
,shallowReactive
,shallowRef
etc. from Vue itself to mark parts of state as non-reactive: https://v3.vuejs.org/api/basic-reactivity.html#reactiveHere is an example, note that adding items to the cart does not show reactively because the
rawItems
is a shallow ref. However when you click Refresh Cart they will appear as the array is reassigned, which triggers reactivity again.