state={
panelData:[
{ title:"title"
content:["one",2]
}
]
}
现在在我的setState中我想添加新的标题和内容,也有我之前添加的数据,所以两个对象对不对? 像这样
panelData: [ {title:"title",content:["one",2]} ,
{title:"title 2"content:[3,4]} ]
我试过这个
handlingAddRecipe(){ //this is my update paneldata function
let contentValue = this.state.recipeContent.join("").split(","); //contentValue = ["content1","content2"]
this.setState({
panelData:[{
title:this.state.recipeTitle, //title:"title1"
content:contentValue //content:contentValue
}]
})
console.log(this.state.panelData)
}
即时获取没有先前数据的输出,只输入传递给paneldata
的数据答案 0 :(得分:2)
this.setState({
panelData:[...this.state.panelData,{
title:this.state.recipeTitle, //title:"title1"
content:contentValue //content:contentValue
}]
})
这应该正确更新