2
如何添加新項子陣列由setState
ReactJS的setState多個嵌套arrays-如何添加新的對象爲子陣列
getInitialState : function(){
return {
Todo:[
{
name:"parimary",
items:[
{item:'Todo itmd #1',isDone:false},
{item:'Todo itmd #2',isDone:true}
]
},{
name:"Secondary",
items:[
{item:'Todo itmd #1',isDone:false},
{item:'Todo itmd #2',isDone:true},
{item:'Todo itmd #3',isDone:true}
]}],selectedItem:"0"};
},
InsertItems : function(newItem){
var item = {item:newItem,isDone:false};
var allItems = this.state.Todo[this.state.selectedItem].items.concat([item]);
this.setState({
Todo: {[this.state.selectedItem]:{items:{
$set: allItems
}}}
});
},
我跑後,我只是得到一個錯誤
Uncaught TypeError: allitems.map is not a function.
如何將一個新對象添加到子數組中?