何时在按钮onClick事件上使用内联函数-Javascript / React.js

Prem

我可以看到将事件处理程序分配给按钮onClick事件的不同样式谁能建议何时在按钮onClick事件处理程序上使用内联函数

onClick={props.handleDeleteOption(props.optionText)}; // Call the handler directly

onClick={(e) => {
          props.handleDeleteOption(props.optionText);
        }} // Call the handler through inline-function
菲利普·托马斯

根据ReactJS文档:

<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>

<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>

以上两行是等效的,分别使用箭头函数和Function.prototype.bind。

在这两种情况下,e代表React事件参数都将作为ID之后的第二个参数传递。使用箭头功能,我们必须显式传递它,但是使用绑定功能时,任何其他参数都将自动转发。

可以在此链接的底部找到:https : //reactjs.org/docs/handling-events.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章