I am trying to update the index of an array inside React state. The problem is that i cannot access the index of the array using setState()
method.
My code looks like this:
handleLike(post) {
axios.get(`/likes/${post}`)
.then(response => {
this.state.posts.forEach((entry, index) => {
if (entry.id === post) {
this.setState({posts[index]: response.data})
}
})
})
}
I expect the array content to be updated with the response data but instead, it throws an error. How can I update the index of an array in React state?