class A extends React.Component{
constructor(props){
this.sendMsg = this.sendMsg.bind(this)
}
sendMsg(){
console.log("Message is send!!!")
}
keyPressed(event){
if (event.key === "Enter") {
this.sendMsg()
}
}
render(){
return(
<input
type="text"
className="form-control input-chat border-0"
id="inputMessage"
placeholder="Type a new message"
onKeyPress={this.keyPressed}
/>
)
}
}
Error: this.sendMsg()
is undefined.
The above code show a component of input box, if the user click enter, I would like to display a console message. But there have a problem occur as above. What need to be added to the code to run it properly?