sascha245 / vuex-simple

A simpler way to write your Vuex store in Typescript
MIT License
180 stars 15 forks source link

Getters in modules are only reactive to a single state variable in that module #33

Closed ericksonc closed 4 years ago

ericksonc commented 4 years ago

Getters in modules are only reactive to a single state variable of the module.

Steps to reproduce:

export class MyModule {

  @State()
  public a: number;
  public b: number;

  public constructor(nb: number = 1, nb2: number = 2) {
    this.a = nb2
    this.b = nb;
  }

  @Mutation()
  public setA(nb: number) {
    this.a = nb;
  }

  @Mutation()
  public setB(nb: number) {
    this.b = nb;
  }

  @Getter()
  public get sumAB() {
    return this.a + this.b
  }
}

In Chrome dev console:

Screen Shot 2020-02-11 at 12 41 04 PM

Although setB is correctly setting the state of "b", the value of sumAB will only be recomputed when the state of "a" has changed.

No matter what, getters in modules can only be reactive to a single piece of state in that module.

Note that, if the code above is placed in the store root instead of a module, it will run as expected, being reactive to both "a" and "b" state.

ericksonc commented 4 years ago

As a workaround, I'm storing all my state in the root store, and only putting getters / actions / mutations in modules. Getters in modules seem to be reactive to multiple pieces of state if they're in root, just not if they're in the module itself.

ericksonc commented 4 years ago

Oops, never mind, so I realized I just needed to add state decorators in front of each piece of state, yeesh