In offical react tutorial they first write class component:
class Square extends React.Component {
render() {
return (
<button className="square" onClick={() => this.props.onClick()}>
{this.props.value}
</button>
);
}
}
and then make it a function component
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}
Why is the onClick change necessary i.e why did we remove the arrow function?
In the first case when we remove the arrow function i.e just keep this.props.onClick() the event happens every time the square renders without even clicking the button and why is it so? ps:I am new to react js , got these doubts when following the tutorial,it would be great if you could give a elaborate answer and any further resources that could help!!thanks in advance.