I am trying to convert my React classes to ES6, but I am having some difficulty within this process.. I would like to have my bindings in the constructor, not in the render view.
Now, if I have a root module with a setState which needs a parameter, e.g.:
constructor() {
super();
this.state = {
mood: ""
};
this.updateMood(value) = this.updateMood.bind(this,value);
}
updateMood(value) {
this.setState({mood: value});
}
Then I pass this function to a component:
<customElement updateMood={this.updateMood}></customElement>
Then within the customElement module, I have something like this:
constructor() {
super();
}
update(e) {
this.props.updateMood(e.target.value);
}
and in the render:
<input onChange={this.update} />
Is this the correct way? Since I can't get it to work ;-(
this.updateMood(value) = this.updateMood.bind(this,value);and change<customElement updateMood={this.updateMood.bind(this)}></customElement>Notice bind this while providing in props.this.updateMood = this.updateMood.bind(this);. You should read the documentation about.bindand about functions in general to get a better understanding about how they work. This has nothing to do with React or ES6 btw.