When you execute onClick={alert("click")}, that will call the function alertand assign the return value ( undefined) to the property onClick. So, what React sees is what it onClick={undefined}says: well, that is not a function, why should I add such a handler?
, onClick, , undefined.
, : onClick={myFunction} myFunction () => alert("..."), , bind :
onClick={alert.bind(window, "click")}
bind , alert "click".
setTimeout(() => alert("after 1 second"), 1000). setTimeout . setTimeout(alert("..."), 1000), alert, setTimeout undefined ( alert).
, , , :
const sayHelloTo = name => {
return () => alert(`Hello ${name}`);
};
setTimeout(sayHelloTo("Alice"), 1000);
onClick thingy:
onClick={sayHelloTo("Alice")}
, ( , , , ):
const elm = {
onClick: null,
click () {
if (typeof this.onClick === "function") {
this.onClick();
}
}
};
elm.onClick = alert("click");
elm.onClick === undefined
elm.onClick = () => alert("click");
typeof elm.onClick
elm.click();