Back

Should you use a lots Pinia Stores?

Should you use a lots Pinia Stores?

To answer immediately, if you are working on a non-trivial app, then you should have multiple stores.

That's a good general guide but if you want to know the reasons or at least a good framework of idea on when to use multiple stores, please read on.

We should start with what does pinia help to do if you have composables or refs and reactives inside your components already? The reason that the pinia website tells you is increased security and the ability to debug easier using the Vue Devtools.

This is the main reason to use lots of pinia stores in my opinion. The ability to know when the state changes and the current state of your components is a big reason to use as much stores as you can. The next question is when do I create a new store and more importantly when NOT to? A good rule of thumb that I'd like to follow is that your app should already be separated by domains. Domains are harder to differentiate honestly but after a couple of medium sized projects you will undoubtedly get used when a module / feature is included in a domain.

This is how it normally should look, the hard part is the differentiation of modules and domains.

> common > store globalStore.ts > domain DomainComponent1.vue DomainComponent2.vue > store domainStore.ts > domain2 Domain2Component1.vue Domain2Component2.vue > store domain2Store.ts

If domain is a bit hard for you, then I suggest to at least separate by features, MVP features. A chat feature for example can be available in multiple parts of your app and that sounds like a good reason to create a separate store for handling states and updating it.