Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuex - what data should be flowing through the store?

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?

like image 723
Garfonzo Avatar asked Apr 17 '17 14:04

Garfonzo


People also ask

Should I store all data in Vuex?

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.

Which part of Vuex is responsible for directly making changes to the data store?

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 .

How much data can you store in Vuex?

store can exceed 500-1000 objects and each of them can have 10-20-30 corresponding images.


1 Answers

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: []
    }
  },
like image 198
Saurabh Avatar answered Sep 17 '22 18:09

Saurabh