Not able to call function from React Js Button Click

class Message extends React.Component {
  constructor() {
    super();
    this.state = {
      message: "This is message",
    };
  }
  changeMessage = () => {
    alert("In");
    this.setState({
      message: "message changed",
    });
  };
  render = () => {
    return (
      <div>
        <h1>{this.state.message}</h1>
        <button onClick={this.changeMessage} color="success">
          success
        </button>
      </div>
    );
  };
}
export default Message;

PS: Bhau, as you said you are new to React then I would highly recommend you Fullstack Open part1 – part4, an amazing resource, and very structured. It will probably take a week to complete and you will learn a lot more than typical Udemy or other tutorial series.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top