Skip to content
Advertisement

Auto update when onSubmit with ReactJS

I have an app that post/get data from json. However when I add a comment, the latest comment that I added did not append automatically, but it will show if i refresh or reinstate the values.

Here’s the code that handles my comments

JavaScript

And this is the component that uses those functions

JavaScript

Advertisement

Answer

It is because, inside handleComment, you are calling this.ReturnComment() without waiting for the POST API to complete.

So GET API fetches the same old data. But as you said if you refresh the page, you will get the new data from API.

So you can do it like this:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement