简体   繁体   English

React:在回调 function 中将事件作为参数传递有多重要?

[英]React: How important it is to pass the event as parameter in the callback function?

<button 
     className="delete-btn"
     onClick={(event) => props.deleteNote(event, note.id)}
>

The code above is for a button that when clicked, calls a function that is in another component and should pass note.id as its parameter.上面的代码用于一个按钮,当单击该按钮时,调用另一个组件中的 function 并且应该传递note.id作为其参数。

To my understanding, when we need to pass a function with extra parameters as props, we need to pass the whole callback function as written above.据我了解,当我们需要传递带有额外参数的 function 作为道具时,我们需要传递整个回调 function 如上所述。

However, I tried this version of code that doesn't include the event as the parameter for the callback function, and it seems to still work.但是,我尝试了这个版本的代码,它不包括事件作为回调 function 的参数,它似乎仍然有效。 Is there an important difference between the code snippets?代码片段之间是否存在重要区别?

<button 
     className="delete-btn"
     onClick={() => props.deleteNote(event, note.id)}
>

It depends on whether you need to do something with the event (like getting data from it or preventing the default action) or not.这取决于您是否需要对事件执行某些操作(例如从中获取数据或阻止默认操作)。 This is my opinion but i like to always pass it for good measure.这是我的意见,但我喜欢总是通过它来衡量。

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

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