Skip to main content
  1. Posts/

Understanding data flow in Vuex

·646 words·4 mins
Joe Erickson
Author
Joe Erickson
Senior software developer specializing in web development, AI, and helping others learn to code.

If you’re like me, when you first ran into Vuex, you probably wondered “How the heck does this work?” It’s not immediately obvious how these types of state management systems work, especially if you come from an SQL background. And do I even need it? In fact, the Vuex documentation has a quote that sums it up pretty well:

Flux libraries are like glasses: you’ll know when you need them.

  • Dan Abramov

Flux, Vuex, Mobx…what are all these “x"es about? Well, at some point the fuzziness of my data became too much and I realized I needed some Vuex glasses. So, I headed over to Vuex site to see what all the hullabaloo was about.

Maybe it’s me, maybe I’m just getting old, but that site did not do a great job of explaining how the heck Vuex worked. It seemed very complex and fiddly and I really didn’t want to use it. All of those Core Concepts and Application Structure and ugh.

Thankfully, I dove into it more and found that it wasn’t all that complex after all. I just needed to break it down in a napkin sketch.

Napkin sketch of Vuex workflow

Tada! Clear as mud!

Maybe I should break this down a little more.

Vuex is a way to keep all your data in one place
#

Your components connect to Vuex

Fundamentally, that’s all Vuex is. This is called having a “source of truth” for your application. Your components will connect to Vuex to access the shared data that lives there. But how exactly does that work?

Data is kept in the State
#

Your component can get data from the state

The data is kept in the state of the Vuex data store. Your component can read that data and, since it’s reactive, update itself when the data in the state updates. Every component in the application is listening to this same state, so everyone stays in-sync.

Data is changed via Mutations
#

Your component can update the state via mutations

Now that the data is in there, you can just directly update it, right?

NO.

One of the important concepts about Vuex is that you should not be changing the state directly. It should only be changed through a mutation. All state changes will happen through mutations. Always. Just remember that and you’ll be fine.

Also, mutations should be very simple, taking data and setting it in the state and that’s it. Anything with any more logic than that will go into actions, as you’ll see next.

Why do you only change state through mutations?
#

Go check out my article on Why should you always use mutations in Vuex. You’ll see some of my thoughts on mutations over there.

Logic operations related to the data live in Actions#

Your component can call actions

Sometimes there might be logic operations that relate to the data, like an AJAX call that pulls data into the application or a generator that creates new GUIDs. That shouldn’t live in a specific component, it’s a data operation so it should be with the data. That’s what the actions are for.

Anything that would be long running should live in an action. Actions, again, never update the state, but they can call mutations that update the state.

Getters are for common filters and formatters for the data
#

Your component can use getters

Getters are used much like computed properties in Vue components, it’s a way to have filtered or formatted data from the state that any component can connect to. Instead of having a computed property in each component, you can create a new getter that components can share and will update when the state updates.

There is the question of should you always use getters when accessing state that I answered recently too. Check that out if you ever wondered that.

So really, just another way of accessing state (but never updating it!).

Hopefully this gives you a better handle on what Vuex is doing if you were struggling with it before. I know it helped me.

Related

Notes and Further Reading for Your Own Personal Bootcamp Talk

·3617 words·17 mins
Welcome to further reading and notes from the Your Own Personal Bootcamp talk. It was great seeing you all at PyOhio this year and I can’t wait to come back again. I hope that, if you were able to see the talk, you were able to pull something away from the talk and had as much fun watching it as I did giving it.

Why should you only change state in Vuex through mutations?

·488 words·3 mins
It’s a common question I’ve seen around and one that I’ve wondered about myself at times. When using Vuex, why is it said that state should only be changed via mutations? Is it really that big a deal? Can’t I just change it directly? It sure seems to work just fine that way.

How to format dates in Vue 2

·726 words·4 mins
Looking for how to format dates in Vue 3? If you’ve used Vue for any length of time, you soon find that it doesn’t have a lot of the fancy formatting options that some of the other frameworks seem to have out of the box. I get the feeling that Vue is very focused on minimalism and adding features that aren’t core to the framework is not something they want to do. One of those features, however, is formatting dates.

Building custom elements from Vue using the CLI

·498 words·3 mins
If you’re working in Vue, the Vue CLI is an amazing thing. There are a lot of moving parts in modern JavaScript development and the CLI makes managing these very easy.

Setting up a Vue CLI project for building Custom HTML Elements

·1082 words·6 mins
I talked about what custom HTML elements were in my last post. Today, I’m going to walk through the process of getting a new Vue CLI project off the ground so that you can build your own. I’ll be using the <my-blink> tag example again, but will be focusing on the step by step1 instructions for creating the environment in which to build and deploy the tags you will be creating.