[英]webpack ReactJS: Unexpected token at this.setState
I am trying to change state of all the elements in an array in ReactJS as shown below. 我正在尝试更改ReactJS数组中所有元素的状态,如下所示。 This is my First application, so I am not able to know what is wrong in over here.
这是我的第一个应用程序,所以我无法知道这里出了什么问题。
closeState(){
this.state.itemList.forEach(function(itemInd){
this.setState({ itemInd.expState : 0 });
}
});
It gives me Unexpected token
error 它给了我
Unexpected token
错误
I have 我有
this.state = {
...........
itemList: [],
.....
}
each element in itemList has : title, desc, expState
. itemList中的每个元素都具有:
title, desc, expState
。
I think you chose wrong way to solve this issue., from your example I understood that you want to set expState
to 0
, for each element in itemList
., if it is true better do it like this 我认为您选择了错误的方法来解决此问题。根据您的示例,我了解到要将
expState
中的每个元素的itemList
设置为0
,如果确实如此,最好这样做
const newItemListState = this.state.itemList.map((itemInd) => {
return Object.assign(itemInd, { expState: 0 });
});
this.setState({ itemList: newItemListState })
In your example you have several mistakes 在您的示例中,您有几个错误
)
after callback function )
You need set this
for .forEach
callback, or use arrow function
您需要为
.forEach
回调设置this
设置,或使用arrow function
2.1. 2.1。
this.state.itemList.forEach(function(itemInd) { }, this);
2.2. 2.2。
this.state.itemList.forEach((itemInd) => { });
You can't use this { itemInd.expState : 0 }
expression. 您不能使用此
{ itemInd.expState : 0 }
表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.