React event handler

Viewed 21

In this React event handling code, is there any semicolon missing? Because when I run it shows missing semicolon in line X:

changemessage () {
  this.setState({ message: " Thanks for sub" })
}
1 Answers

I guess you want:

function changemessage() {
  this.setState({ message: " Thanks for sub" })
}

Or:

changemessage = () => {
  this.setState({ message: " Thanks for sub" })
}

Because the way you wrote it is incorrect (assuming this is not part of a class-based component), it might be expecting a semicolon here, thinking you were trying to invoke changemessage, but that's not the case:

changemessage (); { this.setState({ message: " Thanks for sub" }) }
                
Related