我的狀態有這個json變量:this.state = {
type:
{
name: '',
type2: {
atribute: '',
parameter: [
{
value: '',
classtype: ''
}
],
name: '',
atribute1: '',
atribute2: ''
}
}
}我想要做的是在我的參數列表中添加元素,這些元素在開始時是空的。我做的是這樣的:addParams = () => {
let newParam = {
value: this.state.type.type2.parameter.value,
classtype: this.state.type.type2.parameter.classtype
};
/** */
this.setState(prevState => ({
type: {
// keep all the other key-value pairs of type
...prevState.type,
type2: {
...prevState.type.type2,
//this is supposed to add an element to a list
parameter: [...prevState.type.type2.parameter, newParam]
}
}
})) }但是在執行最后一行代碼時出現以下錯誤:未捕獲的TypeError:在傳播列表時無效傳播非可迭代實例的嘗試我不知道為什么這不起作用,因為參數確實是一個列表。
使用react將屬性添加到屬于某個狀態的列表中
當年話下
2019-04-26 18:15:05