We know about variables and data, We have discussed custom events that trigger parents. We know a thing or two about computed properties and watchers. However, one thing we do not know is how to put all of this together. The funny thing is, all these beautiful feats of data reactivity are limited to their components. They cannot talk on an application scale. Here comes the solution called Vuex.
What is Vuex?
Vuex is a state management library for Vue. It contains variables that are accessible to the entire application. These variables are not reactive by default, but reactivity can be enforced via events called actions and mutations. The whole procedure goes somewhat like the image below.
I’d rather let the code do the talking. You already know that we have a file called store.js. We will now do some editing to the file and you can replicate the code as below.
The above code may not make sense to you but by the time you complete this article, it completely will.
The first few lines, we are simply importing vuex and telling vue to include it in out app.
Next, We are exporting a new instance of the vuex store.
This export is later added to the vue instance from main.js.
Let us now have a look at the code we’ve written inside the vuex store export. Let us discuss the components in detail.
Getters: They are very useful in getting the latest value of a state key. Getters are often used inside computed properties to maintain a live version of a state variable.
Mutations: The state variables inside vuex cannot be modified directly. You need to run these functions called mutations that mutate the vuex state.
Actions: Actions are very much like mutation, they are used for performing actions that involve multiple mutations. Actions are often used to avoid having to write multiple mutation commands.
Now that you know what each item in vuex means, we can actually use this somewhere in our code. Quickly replicate the following code so we can discuss the real application of vuex further.
The most amazing part of what we’ve written in the code above is that we do not have the data property. The reason is simple, we don’t need it here. But we do have a data member that is computed called count. Well, count returns the getter count. Also, since it is a computed property, our local count variable gets updated, every-time the state is changed.
We have added 2 buttons that run 2 previously defined mutations. The syntax is actually self-explanatory. Also, if you had to call them inside the js file, you would use this specifier.
If all goes well, you should see the following output.
You will be able to increment/decrement the count. The flow of events for the same is as follows.
Data from store state called count is loaded into local computed property count.
A button is clicked and a mutation/action is triggered.
The value of count from vuex store state changes.
The new value is sent back to the computed property.
Above steps are repeated until the user gets bored.
Vuex is a great package that can be used for state management. It is officially maintained by the Vue team and has some really interesting features. Initially, you may not feel the need, but when you deal with applications that are more complex, you will need to use vuex. Vuex is a much more complex and this was just a basic tutorial. If you want me to write more articles on vuex or need help with the topics from this article, do let me know in the comment section below.