Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload a component (part of page) in reactjs?

Tags:

reactjs

I was working on react project. I was trying to reload a component when we click the reload button. I implemented the onClick function as given below. But it is reloading the whole window. I just want to reload only that class component, not the whole window. Can anyone help me to solve this?

refreshPage() {
    window.location.reload();
  }
like image 676
Gauranga Avatar asked Jun 18 '19 12:06

Gauranga


People also ask

How to refresh or reload web page in react JS?

React refresh or reload page example; In this tutorial, you will learn how to reload or refresh web page in react js apps wtih javascript window.location.reload () method. And as well as, learn how to refresh react component with hooks. To refresh a page, you need to use the javascript window.location.reload () in React apps.

How to create a page component in react JS?

To run the React app, execute the following command on your terminal: In this step, execute the following command to install react boostrap library into your react app: In this step, create PageComponent .js file. So, visit the src directory of your react js app and create a simple web page component file named PageComponent .js.

How to reload a component using React hook?

By using useState () react hook, we can reload a component too: MyComponent.js. import React, { useState } from 'react'; function MyComponent() { const [ value, setValue] = useState(); const refresh = ()=>{ setValue({}); } return ( <div> <button onClick={ refresh }>Refresh Component</button> </div> ); } export default MyComponent; Copy. That’s it.

What is react JS and why should you use it?

As we know React JS is an open source JavaScript library that is used for building user interfaces specifically for single-page applications. It is also known for providing fast user experience by only updating the parts of the UI that have changed.


1 Answers

use React Hook:

useEffect(()=> {
    fetchData();
}, [data]);
// page will reload whenever data is updated.
like image 87
Ren Avatar answered Oct 07 '22 08:10

Ren