Yeah, I know that setState()
updates a Store's state and emit a change event to all subscribers, but I find out that not everyone uses it and omit it when they update the state.
For example in the following alt-todo repo, they don't use setState()
:
class TodoStore {
constructor() {
this.bindActions(TodoActions);
this.todos = {};
}
onCreate(text) {
text = text.trim()
if (text === '') {
return false
}
// hand waving of course.
const id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36)
this.todos[id] = {
id: id,
complete: false,
text: text
}
}
}
However in the official alt.js repo they use it:
class LocationStore {
constructor() {
this.bindAction(locationActions.updateLocation, this.onUpdateLocation);
this.state = {
city: 'Denver',
country: 'US'
};
}
onUpdateLocation(obj) {
const { city, country } = obj
this.setState({ city, country });
}
}
So I'm wondering what's the difference in the both ways?