Quantcast
Channel: User Just code - Stack Overflow
Viewing all articles
Browse latest Browse all 41

Answer by Just code for The DOM is not updating after my post request with axios in React although the state is updated

$
0
0

You are updating your state wrongly, your code:

console.log(response);const newPost = Object.values(response.data);this.setState({ post: newPost });//this will update the state with new valuesconst updatedPosts = posts.push({title:post.title,content:post.content});//here you arepushing the new values to the existing postsconsole.log(post);console.log(updatedPosts);console.log(this.state.posts);

Desired code:

console.log(response);const newPost = Object.values(response.data);this.setState({ posts:  [...this.state.posts, {title:post.title,content:post.content}]});console.log(post);console.log(this.state.posts);

This way it will update your state after updating the posts array.


Viewing all articles
Browse latest Browse all 41

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>