[英]React transfer event into onClick
I have onClick event that excute fuction with several params. 我有onClick事件,它具有几个参数。 How to add to this paramt current event? 如何添加到此参数当前事件? I want to do in function somethig like this event.stopPropagation()
我想在函数somethig中执行此事件。stopPropagation event.stopPropagation()
My code has the following view (all not nesseccary code was removed): 我的代码具有以下视图(所有非必要代码均已删除):
import React from 'react';
export default class AllCarWashTable extends React.Component{
constructor(props) {
super(props);
this.removeCarWashHandler = this.removeCarWashHandler.bind(this);
...
};
removeCarWashHandler(e, name, id){
e.stopPropagation();
...
}
generateRows() {
const removeCarWashHandler = this.removeCarWashHandler
...
return data.map(function(item) {
var cells = cols.map(function(colData) {
...
<button type="button"
onClick={()=> removeCarWashHandler(item['name'], item['id'])}>
ButtonName
</button>
...
});
}
}
Currenly my code leads to exception: TypeError: Cannot read property 'stopPropagation' of undefined
How to fix this? 目前,我的代码导致异常: TypeError: Cannot read property 'stopPropagation' of undefined
如何解决此问题?
您忘记了将事件传递给函数,可以将onClick事件传递给removeCarWashHandler函数来完成:
onClick={e => removeCarWashHandler(e, item['name'], item['id'])}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.