0
`I am getting the following error

React - uncaught TypeError: Cannot read property 'setState' of undefined


 import React, { Component } from "react";

 class Message extends Component {
   constructor() {
     super();
     this.state = {
       message: "Welcome Visitor",
     };
   }

  changeMessage(event) {
    console.log("i am here");
    this.setState({
      message: "thank you for subscribing",
    });
  }

  render() {
    return (
      <div>
        <h1>{this.state.message}</h1>
        <button onClick={this.changeMessage}>subscribing</button>
      </div>
    );
  }
}


export default Message;

i have created the following code, but I am constantly getting the following errors (lots of them). It looks like the app gets in a loop:

  • [You need to `bind` the class method in the constructor](https://www.freecodecamp.org/news/this-is-why-we-need-to-bind-event-handlers-in-class-components-in-react-f7ea1a6f93eb/). – Andy Dec 20 '22 at 13:09

1 Answers1

-1

you must import useState

 import React, { Component, useState } from "react";