Languages
[Edit]
EN

React - how to refresh the page

3 points
Created by:
Rubi-Reyna
677

In this article, we would like to show you how to refresh the page in React.

There are two ways to refresh the page in React:

  1. using JavaScript,
  2. updating the state.

1. Refresh the page using JavaScript

Edit

To reload the page you can use:

  • window.location.reload() - recommended,
  • window.location.href = window.location.href.

Below we create a simple component that is being refreshed when onClick event occurs.

Note:

Uncomment the second solution to see how it works.

Practical example:

2. Refresh the page by updating the state

Edit

In the below code we create a counter that update causes App component re-rendering with a new state (something like page refreshing). That approach requires to pass couter to child componentes when memo() is used. We can do it with props or contexts. This approach is not recommended because can lead to complicated code structure when many componets are nested.

Note:

For more information read this article: React - useState example.

Runnable example:

References

Edit

Alternative titles

  1. React - how to reload the page
  2. React - how to reload the documment
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join