Why it is need to bind the this keyword in react js. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why it is need to bind the this keyword in react js.

class Hello extends React.Component { constructor(){ super(); this.state = { message: "my friend (from state)!" }; this.updateMessage = this.updateMessage.bind(this); } updateMessage() { this.setState({ message: "my friend (from changed state)!" }); } render() { return <h1>Hello {this.state.message}!</h1>; } }

21st Jan 2021, 3:05 PM
Aman Kumar
Aman Kumar - avatar
1 Answer