[英]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.