I am trying to create a react component which is a text input. When someone pressed enter it must call myMethod(). However in handleKeyPress, I can not access class scope. How can I fix this ?
class MyContainer extends Component { constructor(props, context) { super(props, context); } myMethod(){} handleKeyPress(target) { var self = this; if(target.charCode === 13) { this.myMethod(); } } render() { <input onKeyPress={this.handleKeyPress} ref={(input) => this.inputMax = input} type="text" /> } }
handleKeyPress, put this line in the constructor:this.handleKeyPress = this.handleKeyPress.bind(this)