我可以看到将事件处理程序分配给按钮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] 删除。
我来说两句