简体   繁体   English

反应按钮 onClick 属性:

[英]React button onClick property:

What is the difference between (1) and (2) in React ? React中的 (1) 和 (2) 有什么区别?

onClick={()=>handleDelete(id)} -------(1)

onClick={handleDelete(id)} ----------(2)

why does (2) cause infinite loops whereas (1) works just fine?为什么(2)会导致无限循环而(1)工作得很好?

I couldn't find any documentation on onClick in react being only able to take in functions.我在反应中找不到任何关于 onClick 的文档,只能接受功能。 I'm also a bit confused as to how the Html and JS onClick property is different from react So any documentation links will also be highly appreciated.我也有点困惑 Html 和 JS onClick 属性与反应有何不同所以任何文档链接也将受到高度赞赏。

The code snippet in question:有问题的代码片段:

<button
    className="btn btn--danger"
    onClick={()=>handleDelete(id)}
>
    Delete
</button>

Thanks in advance!提前致谢!

Whatever is in the curly braces is what is returned to the listener.花括号中的内容是返回给听众的内容。 The listener is expecting a function that will be called when the event is fired.侦听器期望在触发事件时调用function

  1. onClick={handleDelete(id)}

This won't work because you're calling handleDelete immediately and assigning the result of calling that function to the listener.这不起作用,因为您正在立即调用handleDelete并将调用 function 的结果分配给侦听器。 That function may return an explicit value or undefined (note: that explicit value may be aa new function (closure) which can be assigned to the listener - but in this case I doubt this is happening). function 可能返回显式值或undefined (注意:显式值可能是一个新的function (闭包),可以分配给侦听器 - 但在这种情况下,我怀疑这种情况正在发生)。

  1. onClick={() => handleDelete(id)}

This will work because you're assigning a function to the listener, and when the event is fired it will call that function which, in turn, will call handleDelete(id) .这将起作用,因为您将 function 分配给侦听器,并且在触发事件时,它将调用 function ,而后者又将调用handleDelete(id)

  1. onClick={handleDelete}

This will also work because you're passing a reference to the handleDelete function to the listener, and that function will get called when the event is fired.这也将起作用,因为您将对handleDelete function 的引用传递给侦听器,并且在触发事件时将调用 function。

(Note: doing it this way would mean that the component would need to be rewritten to have a data-id attribute that the function can pick up because you're no longer sending an explicit id argument to handleDelete when you call it.) (注意:这样做意味着需要重写组件以具有data-id属性,function 可以获取该属性,因为您在调用它时不再向handleDelete发送显式id参数。)

before explaining the differences you have a mismatching braces in the second one在解释差异之前,您在第二个中的大括号不匹配

The second one is Called invoked function that means when the component mounts the function runs without clicking the button, it will invoke and runs it, and does not wait and will run immediately,and for the other one is callback function it will run when the user click the button The second one is Called invoked function that means when the component mounts the function runs without clicking the button, it will invoke and runs it, and does not wait and will run immediately,and for the other one is callback function it will run when the用户点击按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM