DEV Community

Cover image for Should you always use getters in Vuex?

Should you always use getters in Vuex?

Joe Erickson on August 09, 2019

One of the questions that comes up time and again concerning Vuex is "Do I always use a getter when accessing data? Or can I directly access the ra...
Collapse
 
atlesque profile image
Alexander Van Maele • Edited

Great post Joe, thanks!

I agree there is no need for getters if you're accessing a basic, non-computed state. To me, 'basic' means one or at most two levels deep into the state. e.g.
state.selectedOrder.deliveryTime

However, when accessing beyond two levels, I generally like using a getter. So state.selectedOrder.deliveryTime.date becomes getters.deliveryDate

This makes it easier to read components which are using this state imo. If you combine this with ...mapGetters[] you can write very succinct components like:
<span>{{ deliveryDate }}<span>
Instead of:
<span>{{ $state.selectedOrder.deliveryTime.date }}<span>

Collapse
 
firstclown profile image
Joe Erickson

Oh, really good point! I'm going to work on rolling this into the main text, if you don't mind (crediting you, of course). I wouldn't want others to miss it.

Collapse
 
atlesque profile image
Alexander Van Maele

That's great! Feel free to! 😊