Skip to content
Advertisement

How can I get the updated data without refresh the page in React?

I have a component with create a blog. It works fine and navigates me to the home after adding the new blog object in the database, but I have to refresh the page to be able to see the updated data. So how can I see the updated blogs after navigation without refreshing the page?

JavaScript

Advertisement

Answer

In order to update the blogs state, which is declared at the main point of the application, which is, for example, App.jsx, you must pass the state blogs and the set-state-action setBlogs as properties to the child component. For example:

The state in App.jsx:

JavaScript

The child component (on which you will update the blogs state):

JavaScript

On the fetch‘s then

JavaScript
Advertisement