我有onClick事件,用几个参数执行功能。如何添加到此paramt当前事件?我想在函数somethig中做这样的event.stopPropagation()
我的代码有以下视图(所有非nesseccary代码都被删除):
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我的代码导致异常:TypeError: Cannot read property 'stopPropagation' of undefined
如何解决这个问题?
答案 0 :(得分:0)
您忘记将事件传递给函数,可以将onClick事件提供给removeCarWashHandler函数:
onClick={e => removeCarWashHandler(e, item['name'], item['id'])}