0

Imagine the following medium/large App using vue 2 (With vue 3 this would be easy to answer thanks to the composition api), where we got App.vue with X amount of components:

enter image description here

Suppose that "Children c" and "Comp A" use the same state (or single source of truth like some people say). Having said that, we could use vuex to store the state in one place, the problem with that is that now we have a global state for some data that needs to be shared between two distant components, but to be honest I just don't know if that's a good practice.

Now if the same need appear with different states between some components, we would have to repeat the same approach, creating many globals vuex stores for some components that need to consume the same data.

Now we also have Vue 2.6 Observables, these observables does basically the same as vuex without the vuex dev tools support and is already built in in Vue 2, but there is not enough documentation about it. This is a quote from Thomas Findlay's book vue- the road to enterprise:

enter image description here

What do you think, is vuex really worth it in vue 2 for a medium/large application?

Damian Perez
  • 534
  • 4
  • 10
  • my answer [here](https://stackoverflow.com/a/57712105/8172857) gives an example of obeservable, and for the case component A and C you could use the dependency injection – Boussadjra Brahim Jan 21 '21 at 21:58
  • If you don't see the benefit of having one source of truth, you haven't developed enough. Between Vuex and `Vue.observable()` they're similar when all you want are synchronous mutations, but Vuex also has actions, which allow you to wrap much more complex logic, promise based. The real trick in using one source of truth is balancing logic between what is the general store and each component's internal reactive state (`data`). Overusing the general store can be bad, as can be not using it enough (or not having one at all). – tao Jan 22 '21 at 00:11

1 Answers1

1

Generally, Vuex or Global state is only necessary when children of different parents need to access the same data. For example authentication is a good one to have in global state if the application has more than one branch.

Using the event bus is effective but once the application grows to a certain point, it can become convoluted and difficult to understand where the data is flowing to which components.

One should be diligent in determining what data actually needs to be global because there is a price to pay with overhead.

I understand your resistance, globals are ugly, however many developers choose to use Global state management so that it is there need be.

knary
  • 301
  • 2
  • 5