Author -  Sai gowtham

How to refresh the Page or Component in React

In this tutorial, we are going to learn about how to refresh a page or component in React with the help of examples.

Refreshing a page

To refresh a page, we need to use the window.location.reload() method in React.

By default this method reloads the page from a cache, if we pass true as an argument it reloads the entire page from a server instead of cache.

Let’s see an example:

Home.js
import React from "react";

function Home() {

  const refreshPage = ()=>{
     window.location.reload();  }

  return (
    <div>
      <h1>{Math.random()}</h1>
      <button onClick={refreshPage}>Refresh</button>    </div>
  );
}

In the above code, we are refreshing the page by clicking on a refresh button.

Refreshing a component

If you want to refresh the particular component instead of an entire page, you need to call the this.setState() method with an empty object.

App.js
import React from "react";

class App extends React.Component {

  handleRefresh = () => {
    // by calling this method react re-renders the component
    this.setState({});  };

  render() {
    return (
      <div>
        <h1>{Math.random()}</h1>
        <button onClick={this.handleRefresh}>Refresh component</button>      </div>
    );
  }
}

export default App;

By calling this.setState() method with an empty object react will think that something is upated in the state and component needs to be refreshed (or re-rendered) with a new UI.

Refreshing a component using hooks

In react hooks, we can use the useState() hook to refresh the component.

import React from "react";

function Home() {

  const [value,setValue] = useState();

  const refresh = ()=>{
      // it re-renders the component     setValue({});
  }

  return (
    <div>
      <p>{Math.random()}</p>
      <button onClick={refresh}>Refresh component</button>
    </div>
  );
}

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY