import React from "react";
const FunctionClick = () => {
const clickHandler = () => {
console.log("Button clicked.");
};
return (
<div>
<button onClick={clickHandler}>Click</button>
</div>
);
};
export default FunctionClick;
{REACT NOOBIE ALERT} In the above code, I can see the difference in the output of
<button onClick={clickHandler}>Click</button>
and
<button onClick={clickHandler()}>Click</button>
but can someone explain why we are passing the function and not calling it?
How is the function (in second code) being called when the page loads even though we want to call it when the button is clicked.
clickHandleris similar than() => {return clickHandler()}. ButclickHandler()executes the function on every renderfunction name() { return func() }is not same asfunc()