I'm simply trying to append a json created dynamically into an array that lives inside the state.
The only way that I've found till now is to use concat, because push doesn't work.
constructor() {
super()
this.state = {
list: []
}
this.add = this.add.bind(this)
}
add(e) {
e.preventDefault()
var task = document.getElementById('task').value
var json = JSON.parse(JSON.stringify({key: task, item: task}))
this.setState({list: this.state.list.concat(json)}) // WORKS
// this.setState({list: this.state.list.push(json)}) // DOESN'T WORK
console.log(this.state.list)
document.getElementById('task').value = ''
}
That works, but I don't understand why I have an empty array in console after the first item it's created. I've read how concat works, but obviously I missed something. Why I can't push an object inside my this.list array?