Ultimately I want an array of objects to render, so I start by initializing state.
state = { entries: [] };
Then I create a method that accepts an object input by the user and adds it to my array of objects.
onEntrySubmit = entry => {
console.log(entry);
console.log(this.state.entries);
this.setState({ entries: [...this.state.entries, entry] })
console.log(this.state.entries);
};
The object being passed in appears to be correct, but instead of adding this object to the array, this code replaces all existing objects with the new object. Why is this happening?
I have also tried passing the existing state to a variable and using the push method, but that did not work either.
let temp = this.state.entries;
temp.push(entry);
this.setState({ entries: temp });
Here is a sandbox I made: https://codesandbox.io/s/weight-tracker-9rz95?file=/src/components/App.js