[英]DIV onClick Pass Array to Function in ReactJS
I am getting the following error我收到以下错误
Uncaught Invariant Violation: Expected
onClick
listener to be a function, instead got a value ofobject
type.未捕获的不变违规:预期
onClick
侦听器是一个函数,而不是获得object
类型的值。
when I try to pass a value (which is an array) to a function when clicking a div.当我尝试在单击 div 时将值(这是一个数组)传递给函数时。 If this can not be done, what would be the best way to handle this?
如果无法做到这一点,处理这个问题的最佳方法是什么?
itemsArray(arr) {
let items = [];
let self = this;
const grouped = this.groupBy(arr, item => item.source);
grouped.forEach(function(value, key) {
switch(key.toString().toLowerCase()) {
case "occ_al": return items.push(<div onClick={self.listAllActivities(value)} className="recentActivityChannels"><img src={occ} width="15" /></div>)
}
})
items.push(<div className="clearFloat"></div>);
return items;
}
listAllActivities(arr){
let items = [];
var self = this;
const grouped = this.groupBy(arr, date => getDateServiceTab(date.date));
grouped.forEach(function(value, key) {
value.map((map) => {
//removed as its not important
return items;
}
You are executing the function listAllActivities
, and then the function returns an object.您正在执行函数
listAllActivities
,然后该函数返回一个对象。 You could use an arrow function to solve this like this:您可以使用箭头函数来解决这个问题:
onClick={() => self.listAllActivities(value)}
An alternative way to calling with an arrow function使用箭头函数调用的另一种方法
onClick={() => function()}
is to define it as an arrow function是将其定义为箭头函数
itemsArray = (arr) => {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.