React bind in constructor, how to pass parameters to props React bind in constructor, how to pass parameters to props reactjs reactjs

React bind in constructor, how to pass parameters to props


You can't use this this.updateMood(value) = this.updateMood.bind(this,value); construction, because it is syntax error.

You can solve your problem like this

class CustomElement extends React.Component {  constructor() {    super();    this.update = this.update.bind(this);  }  update(e) {    this.props.updateMood(e.target.value);  }  render() {    return <input onChange={this.update} />  }}class Parent extends React.Component {  constructor() {    super();    this.state = {        mood: ""    };    this.updateMood = this.updateMood.bind(this);  }  updateMood(value) {    this.setState({ mood: value });  }  render() {    return <div>      <CustomElement updateMood={this.updateMood}></CustomElement>      <h1>{ this.state.mood }</h1>    </div>  }}

Example


Or, depending on your babel settings, or when using typescript, the following achieves the same but is a lot more convenient to write / maintain:

class Parent extends React.Component {  constructor() {    super();    this.state = {        mood: ""    };  }  updateMood = (value) => {    this.setState({ mood: value });  }}