goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 323 forks source link

Question: What is the correct way for setting values in an alt store? #595

Closed weblogixx closed 8 years ago

weblogixx commented 8 years ago

Hi @goatslacker,

first of all, let me thank you for this software. Alt really rocks and makes it much easier to create flux based applications.

I am a little bit confused about the different ways of setting values for stores. On your example pages, the data is set in the following way (which does work quite well):

class ExampleStore {
  constructor() {
    this.something = 'x';
  }

  handleSetX(data) {
    this.something = data;
  }
}

This is the way I am currently using alt. Sometimes I also do multiple changes like

class ExampleStore {
  handleMultiple(data) {
    this.x = data.x;
    this.y = data.y;
  }
}

I have also read in some comments that the whole state should reside in single keys on the store class, but in a defined state property named state:

class ExampleStore {
  constructor() {
    this.state = {
      a: 'b'
    };
  }
}

In these cases, it is also said that the method setState() should be used to update the state changes.

So my question is: What is the fundamental difference between those two assigns? Both seem to work without problems and are easily testable. However, in the getting started section the first variant is used, not hinting on the setState one.

jdlehman commented 8 years ago

Hey @weblogixx, both ways are completely valid and I would say that you can use what feels right based on your personal preference.

That said, setState was introduced to stores for a few reasons.

So tldr; use whichever feels right to you, they both will get the job done, but storing store data on this.state is probably best if you plan on using utilities like the immutable util