I'm trying to create the following structure in React
{
items:
{Dogs: [{name: "Snoopy"}, {name: "Lola"}, {name: "Sprinkles"}],
Cats: [{name: "Felidae"}, {name: "Garfiled"}, {name: "Cat in the Hat"}]
}
Using the following function:
handleAddItem(s) {
var key = Object.keys(s)[0];
var value = s[key];
var allItems = {...this.state.items};
allItems[key] = allItems[key];
allItems[key].push({name: value});
var ourItems = {};
ourItems = allItems[key];
ourItems.push({name: value });
// console.log(ourItems);
// console.log(allItems);
this.setState({items: allItems});
}
Why are the key value pair being added to state twice?
dogandcatare typos? shouldn't be in pluraldogs,cats?