1

I have Message.js -

import React from 'react';
import { Button } from 'reactstrap';
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

Button click is not working here. I tried with -

<Button onClick={changeMessage} color="success">success</Button>

Then Also Tried -

<Button onClick="changeMessage()" color="success">success</Button>

This also did not work.

enter image description here

Uncaught TypeError : this.props.onClick is not a function.
1
  • In your case, you should add the binding function in the constructor just like this. constructor() { ... this.changeMessage = this.changeMessage.bind(this) ... } or you can define the changeMessage function as the arrow function just like this. changeMessage = () => { ..... } Commented Nov 4, 2020 at 5:34

6 Answers 6

1
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.

Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to put quotation for functions in reactjs.

Just assign like this.

<Button onClick={this.changeMessage} color="success">success</Button>

and function

changeMessage = () => {
        alert("In");
        this.setState({
            message :"message changed"
        });
    }

Comments

1

No inverted commas

<Button onClick={this.changeMessage} color="success">success</Button>

Please bind the function in the constructor or use arrow functions.

4 Comments

Also changeMessage() { should be changeMessage=()=> { as he is using this.state inside that
I think he also has to do function binding in the constructor.
Yes, I am mentioning that too
@KetanRamteke barobar aahe tumch , mi navin aahe yatt tyamule kahi chuka rahat aahet.
1
<Button onClick={this.changeMessage} color="success">success</Button>

<Button onClick={() => {this.changeMessage(your params)}} color="success">success</Button>

Comments

1
onClick={this.changeMessage} need not to be a string .

Try this :

<Button onClick={()=>this.changeMessage()} color="success">success</Button>

Comments

1

The onClick you passed should be a function and the function has to be bound to use "this" pointer inside the "changeMessage" function, so you can try using as follows,

<Button onClick={this.changeMessage.bind(this)} color="success">success</Button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.