I'm struggling with setting the state of one of my objects in React. The code which I have:
export class RRHN_App extends React.Component {
constructor(props) {
super(props);
this.startLoadingItems();
this.fetchData();
this.state = {
items: [],
itemStatus : [],
selectedItem: null,
prefsDialog: false,
preferences : {
color : "brown",
listSize : 800
}
}
}
toggleItem(item) {
console.log("Selected item: ", item);
if( this.state.selectedItem &&
item.id === this.state.selectedItem.id) {
this.setState({selectedItem: null});
} else {
let ItemID= item.id;
let newSeen = {ItemID : "seen"};
this.setState({selectedItem: item});
this.setState({itemStatus: newSeen});
}
console.log(this.state.itemStatus)
}
fetchData() {
request.get("http://localhost:3000/itemStatuses")
.end( (err,response) => {
this.setState({itemStatus: response.body})
})
}
startLoadingItems() {
request.get("http://localhost:3000/hn/topstories")
.end( (err,response) => {
this.setState({items : response.body})
})
}
}
When printing the itemStatus
I get this:
{16490176: "seen", 16497964: "seen", 16514428: "read", 16542395: "read", 16566536: "read"}
When the code is executed the itemStatus
looks like this:
{ItemID: "seen"}
How can I add a key-value pair of the item.id and "seen" to the state?