Giter Club home page Giter Club logo

Comments (3)

LinusBorg avatar LinusBorg commented on June 14, 2024 1

I just want to know how you guys deal with this. Please give me insights as I really need to mutate the state by doing:

state = {
   ...newState
};

Well, even if you get eslint to allow this (you can simply disable the rule) - this will do nothing to the state in vuex. You just changed the reference of the local variable called state inside of the mutation - the state object that this variable pointed to before this change is not affected.

You can't completely replace the original root state (of vuex as a whole or a store module) with a new one. Vuex is not built around immutable principles because they are orthogonal to the Reactivity that Vue is built upon, so you have to mutate the state.

However, you can overwrite attributes like this:

Object.assign(state, newState)

If you really need to completely replace a piece of state, then save that original piece of state as a property, not as root properties:

// in your store (module)
const state = {
  someProp = {
    /* this is the object you wnt to replace */
  }
}

const mutations = {
  SOME_MUTATION: (state, newState) => {
    state.someProp = newState
  }
}

from pwa.

aprilmintacpineda avatar aprilmintacpineda commented on June 14, 2024

Thanks.

from pwa.

rsmithlal avatar rsmithlal commented on June 14, 2024

This should help: vuejs/vue-cli#3954 (comment)

from pwa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.