I am new to react and learning it via reactjs.org "Tic Tac Toe" tutorial, i have question on below.
function Square(props){
return(
<button className="square"
onClick={() => props.onClick()}>
{props.value}
</button>
);
}
Can be written as
function Square(props){
return(
<button className="square"
onClick={props.onClick}>
{props.value}
</button>
);
}
However below is incorrect
function Square(props){
return(
<button className="square"
onClick={props.onClick()}>
{props.value}
</button>
);
}
Can someone explain why "onClick={props.onClick()}
" is incorrect however both "onClick={() => props.onClick()}
" and
"onClick={props.onClick}
" are correct.?
When using "onClick={props.onClick()}
" it compiles fine however react throws below error at run time.
Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.