3
I am learning Act and 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 summary is a button that when clicking displays a message. But one thing I didn’t understand is this statement from handleClick
, For if I so pronounce:
handleClick () {
alert(this.state.message);
}
The compilation does not give error, but clicking gives the following error. I am not sure the purpose of this statement. Someone could clarify this doubt?
Read the tutorial again, it looks like you skipped a step. Is this? https://reactjs.org/docs/react-without-es6.html
– Leandro Angelo
Yes, I’m reading, but I’m not getting to know the difference between declaring as
handleClick(){};
andhandleClick = () => {};
.– Vinicius Morais