[英]Call multiple function with parameters
我有一个带有作为属性传递的参数的函数:
currentTaskID = null;
setTaskID = (id) => {
this.currentTaskID = id;
console.log(this.currentTaskID);
console.log("test");
}
然后在我的另一个组件上,我有一个需要事件处理程序并在单击时调用 prop 函数的函数:
// select/open task
openTask = (event) => {
let targetClass = event.target.classList;
if(targetClass.contains('active'))
targetClass.remove('active')
else
targetClass.add('active')
}
// onClick trigger multiple functions
funcWrapper = (id) => {
this.openTask();
this.props.setTaskID.bind(this, id)
}
点击:
onClick={funcWrapper(id)}
错误:
Cannot read property 'target' of undefined
如何在不覆盖事件的情况下传递参数?
由于错误提示无法读取 undefined 的属性 'target' 。 它无法在undefined 中找到target
属性。 因为target
是event
对象的一个属性。 因为event
尚未传递给方法,所以它用 undefined 表示。
我想你必须在匿名回调中进行调用:
onClick={e => funcWrapper(e, id)}
然后将事件传递给方法:
funcWrapper = (ev, id) => {
this.openTask(ev);
this.props.setTaskID(id); // <---as it is in fat arrow syntax no need for bind
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.