DatsunBing
DatsunBing

Reputation: 9076

Vuex: when to use state versus getters?

When retrieving data from Vuex, in some cases the choice between using getters and access state directly is quite clear. For example:

But what about other cases where there is only a slight amount of logic required to retrieve a value? For example, what if I have a foo object in the root of my store, and:

Either of these needs could be met using a mapState function, but should I be using mapGetter instead? What's the difference?

Upvotes: 7

Views: 1883

Answers (1)

Eric Guan
Eric Guan

Reputation: 15982

Either of these needs could be met using a mapState function

True... but what happens if more than 1 component needs the "slightly altered" value? You'd end up with duplicate logic. In that case perform the "slight amount of logic" in a getter.

Upvotes: 2

Related Questions