Check out a free preview of the full Vuex for Intermediate Vue 2 Developers course

The "Getters" Lesson is part of the full, Vuex for Intermediate Vue 2 Developers course featured in this preview video. Here's what you'd learn in this lesson:

Divya explains that getters are computed properties cached in the event that a state does not change and only update when a mutation occurs, and goes through an example of a getter.


Transcript from the "Getters" Lesson

>> The next piece of what I wanted to look at is this, the part about getters, which is something we didn't actually talk about, but we will now. So we have actions and mutations, we talked about that in length in terms of an action creates an asynchronous fetch, which is pinging inventory or fetching from inventory and then it commits specific mutation.

And so if you looked at the stock counter, that is the supply or the change in which the supply gets changed, so you see a decrement, increment and so on. But the thing is, it's useful for us to perhaps add a layer of information on top of it.

So we want to perhaps add a layer of information over the specific supply itself. So right now we have this action that happens and then the mutation changes the specific supply. And then we looked at the code specific to that. Just the idea from lines 22 to 30 is where we're actually fetching inventory and then updating specific properties and so on and mutating.

But of course, we want to look at getters specifically in detail because we talked about actions and mutations. We didn't really talk about getters, we sort of glossed over that. And so let's look at that particular piece of Vuex because it can be really useful in our applications.

A way of thinking about getters is that it's a way of computing derived state. What I mean by that is it allows you to look at your state within the store and then check for certain properties. So something that might be useful is you might want to check whether or not your supply is low.

I shouldn't have started 40, there you go [LAUGH] terrible top, it should have been 10 never mind, we're here now. So you might have noticed that I have added a sort of a caution icon onto my supply and what this is doing is it's checking to see whether supply is below a certain number, and if supply is below a certain number, it shows this specific thing.

And so for this I'm using a getter. What it's doing is that specific getter, which shows this caution icon is just looking at my state, in this case, the supply in the state. And it's checking to make sure that it doesn't fall below a number and if it does, then it tells the front end that something has changed and you sort of see that bubble up.

So that's really useful. And so this is where a getter comes in. You have two pieces, we looked at state which is state gets changed as actions happen and mutations happen within your store. But you can also add a layer of information like a getter, so the getter checks for state and then adds extra things to your front end that can be really useful.

So let's look at what that looks like from our application itself. So, we'll start by looking at our store specifically, and we'll add a getter to our store. So on line 13, I created getter called, is supply low. Personally, I like to call my getters as sort of like a question because oftentimes you can think of a getter as you're asking your store what's in the state.

So is supply low is like is supply low, and then the getter will tell you yes it is or it might tell you what exactly the thing you're asking for is. That's a useful way in which I name my getters just for a fun way of thinking about it.

So in this particular case I called it isSupplyLow. And so isSupplyLow is a true, false question. So for that I'm just checking specifically whether or not the supply has fallen below 10. So is below 10 and it's just returning true or false the state at which that is.

And then from the component itself, I'm creating again, we're using computed properties because, a getter very similar to state gets updated as mutations happen, because a getter is checking state, state gets changed very commonly. And so getters also are changed very similarly. And so I'm using a computer property because I want my view instance to always be aware when something happens like state, or getters in this case.

And so I'll call my getter isSupplyLow. And then I will essentially check the getter in the store. And again, very similar to checking for state you're doing this .dollarstore.getters dot name of the specific getter. So the getter is called isSupplyLow. So I'm doing this And all that's doing is it's returning that true or false that I can then use in my front end itself.

So that's a really useful way in which I can use a getter to propagate some kind of attribute or show some data from my front end itself. And the reason I use a getter is because let's say I have multiple different components that all show various inventories. I want to be able to reuse that piece of logic.

You could easily write this in your component itself, but it's useful as a getter because it's specifically checking the store. So there was a question in chat about why getters are cached. I sort of glossed over that. But I did mention that getters are cached. But something you may have noticed is that getters are constantly updated as state changes.

And so the reason I say it's cached is it's cached in the event that state doesn't change. So a computer property generally is cached unless something changes to update itself. So that's why I say it's cached, it's not cached always as in, it's not cached and never updated, it's cached when updates don't happen.

So you're not constantly querying state itself. So for instance if state isn't changing, the getter is not constantly checking and asking the state whether or not it changed if nothing's changed.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now