[英]Why doesn't React render an element without function invoked by arrow function unlike normal onClick functions?
Why does this work?为什么这行得通?
<span onClick={()=>removeTodo(index)}>x</span>
But this doesn't work?但这不起作用?
<span onClick={removeTodo(index)}>x</span>
Full code完整代码
{todo.map((item, index) => {
return (
<pre className={index} key={index}>
<h1 id={array}>{item}</h1>
<span onClick={()=>removeTodo(index)}>x</span>
</pre>
);
})}
The onClick function requires a short hand function syntax ()=>{}
especially when you are calling a function and passing the parameters at the same time. onClick 函数需要简写函数语法
()=>{}
,尤其是在调用函数并同时传递参数时。 It is also used when you are calling multiple functions inside onClick.当您在 onClick 中调用多个函数时也会使用它。
When to Use ()=>
in onClick?何时在 onClick 中使用
()=>
?
When not to use?什么时候不使用?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.