I'm learning react and I came across this example (which works normally):
import React, { Component } from 'react'
import './Teste.css';
class Teste extends Component {
constructor(props) {
super(props);
this.state = { message: 'Hello!' };
}
handleClick = () => {
alert(this.state.message);
}
render() {
return (
<div>
<button onClick={this.handleClick}>
Say hello
</button>
</div>
);
}
}
export default Teste;
In short, it is a button that, when clicked, displays a message. But one thing I did not understand is this statement of handleClick
, because if I declare it this way:
handleClick () {
alert(this.state.message);
}
Compilation does not give error, but clicking gives the following error. I do not understand the purpose of this statement. Could someone clarify this doubt?