I understand what the store is and the purpose of Vuex, but now I'm thinking:
"What do I actually put in the store?"
The SPA I'm building handles a lot of data. Initially I was only putting in the "main" stuff. However, is that wrong? Should the store
actually hold all data for the SPA? If I present data on the webpage, should that be flowing through the store
?
If you really need to have the data stored somewhere (vuex or components) it will have to use memory space anyway. I prefer using Vuex for large applications since it allows me to compartmentalize the data and have it structured in a way that is easier to understand and more organized.
The second key defined in the store is the mutations key. As the name suggests, it contain an object of all of the properties responsible for making changes to the state .
store can exceed 500-1000 objects and each of them can have 10-20-30 corresponding images.
As a rule of thumb, is the data is only used by one of the component, or it is one way propagated to child, you should be okay with having that at component level.
But if data is changed and accessed by more than two components it can be put in centralised state: that is vuex.
Quoting from the docs:
if you are building a medium-to-large-scale SPA, chances are you have run into situations that make you think about how to better handle state outside of your Vue components, and Vuex will be the natural next step for you. There's a good quote from Dan Abramov, the author of Redux:
Flux libraries are like glasses: you’ll know when you need them.
You can also refer vue-hackernews example, where you can see lists, users are in vuex store, while components also have data while are specific to only that component.
data in component:
data () {
const data = {
loading: false,
transition: 'slide-up',
displayedPage: isInitialRender ? Number(this.$store.state.route.params.page) || 1 : -1,
displayedItems: isInitialRender ? this.$store.getters.activeItems : []
}
isInitialRender = false
return data
},
state in vuex:
state: {
activeType: null,
itemsPerPage: 20,
items: {/* [id: number]: Item */},
users: {/* [id: string]: User */},
lists: {
top: [/* number */],
new: [],
show: [],
ask: [],
job: []
}
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With