Help, I cannot seem to figure out how to solve this problem:
array.map(object => {
console.log(this.state.newArr) // [] didn't update despite setState
if (this.state.newArr.length === 0) {
axios.get('http://localhost/')
.then(res => {
this.setState(prevState => ({ newArr: [...prevState.newArr, res] })) // setState here
})
}
})
Edit: updated code implemented the answers, but still doesn't work atm.
componentDidMountonly runs one time. It will not run again after your state is updated.setStateis async, so it will no be using the updated value when it sets it. I'll answer with an example