Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I put state initialization code in Vuex?

Should Vuex store keep only the code of state structure and how to modify that state (mutations, actions), or also the actual state initialization and values?

I started wondering about it, when my state initialization code become more complex, because I don't see any natural place in Vuex architecture to put this code into.

Let's say I have such a store:

export default {
  state: {
    list: []
  },

  mutations: {
    addItem(state, { item }) {
      state.list.push(item);
    }
  }
}

If the list starts empty, it's enough. But what if I have default values for the list and also want to store the list in LocalStorage, so that I can preserve it's value between page loads.

const STORAGE_LIST_KEY = 'list';

const LIST_DEFAULT = [
  {
    id: 1,
    name: 'item 1'
  },
  {
    id: 33,
    name: 'item 33'
  }
];

function initializeList() {
  let savedList = localStorage.getItem(STORAGE_LIST_KEY);
  return savedList ? savedList : LIST_DEFAULT;
];

Is there some natural place in the Vuex store architecture, where I should put initializeList() function? (e.g. In a Vue component I would put initializeList() into methods part of the component) Or maybe the store is just about the structure and the initialization code belongs to the Vue components?

like image 706
Robert Kusznier Avatar asked Jun 22 '18 10:06

Robert Kusznier


People also ask

Where is Vuex state stored?

At the center of every Vuex application is the store. A "store" is basically a container that holds your application state. There are two things that make a Vuex store different from a plain global object: Vuex stores are reactive.

How do I change my initial state in Vue?

You can create a function that returns the initial state, and use it into your Vuex instance, like this: function initialStateFromLocalStorage() { ... const empty = { status: '', token: '', user: null } return empty; } export default new Vuex.

How do I add data to my Vuex?

To create new data, you can use the insert , create , and new methods. They all insert new records to the Vuex Store but behave in a slightly different manner. The insert method will simply insert new records. You should pass an object containing records in data key to the method.


1 Answers

Your initialization state will be some sort of mutation or action depending upon the needs of async and side effects. This actions should be fired only once during initialization.

This mutation/action will then be triggered from root instance or sufficiently higher level component. You can also split the initialization of entire application state into smaller mutations or actions.

The advantage of this approach is you can split your Vuex store and dynamically load Vuex module (lazy loading).

In your case, since you wish to inflate/deflate state from local storage (which is a side effect), it makes sense to make an action.

like image 176
Harshal Patil Avatar answered Sep 18 '22 18:09

Harshal Patil