Vuex — how to use state

Harry Beckwith
Vue.js Developers
Published in
3 min readJul 24, 2019

When an app becomes complex in terms of where data is being passed between multiple components, it is time to make use of Vuex.

Vuex is a state management pattern + library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.

Why use Vuex?

  • Vuex provides one source of truth
  • Every component has access to the global state
  • The global state is reactive, just like local state
  • Reduces component data tracking from becoming complex.
  • Vuex provides a state pattern to make updates standardized.

To set up Vuex you have to install the npm package.

npm i vuex --save

Inside the src folder, we can now set up a store.js file.

Heres how the store.js file would look.

Inside main.js we can link the store to the Vue instance by importing the store file and added to Vue…

Right now we have the store set up 🥳 but we cannot access or edit yet 😕.

How to work with the store (vuex)

Mutations

  • Commit + track state changes
  • The best practice is for actions to call mutations then mutations update the state directly.
  • Using the dev tools we can roll back mutations back to the previous state value.

Getting the state from Vuex into computed properties.

Inside the store.js we can create getters, these interact directly with the state in the store. They are used to get the state. We can then display the getters inside components through computed properties.

Documentation for getters here.

Store.js file with state and getters set up.

How to link the getters to components?

Use computed properties, but first, we can map the getters for easy access like so.

You have to import { mapGetters } from Vuex to be able to place inside the computed properties. Using the spread operator and pacing each getter as a string array item we can then reference individual getters inside the template.

We can also create getters that take arguments which are then used as a computed property and take input inside the template. The example for this is the getter getEventById in the above two code blocks.

Mutations

  • Can update/mutate Vuex State.
  • They are synchronous, they happen one after another.

Mutations documentation here.

Actions

  • Are asynchronous, they may not happen in the order they appear in the code.
  • Can wrap business logic around mutations
  • Always put Mutations within Actions — as business logic, later on, would be easy to apply, this increases the scalability.

Actions documentation here.

Here we can see that mutation has been created that takes an event and pushes to the events array. The action is the code that we will dispatch to from the component, in turn, this calls the mutation and updates the store state.

Let's see how to call the action inside a component.

this.$store.dispatch('createEvent', this.event)

Referencing the store and dispatch with the name that matches actions function name, this will now send the this. event data into the action, then call the mutation and update the state.

Using the pattern above is a way to set up and use Vuex as the data point in your application was helps avoid tracking through components to see where the data needs to pass to. This can become bloated in larger applications so a module approach is needed to aid larger apps. Documentation can be found here.

That’s the basics of Vuex, cheers ☕️

--

--