I have a web app with multiple pages and routing, in every single page I have table with lots of data, so every time I change page and press back button, the history page is reloads itself.
I want to keep the data of ListingPage as it is when I goBack to ListingPage from DetailPage. But every time it calls getAllRecords function and reloads the table.
I tried changing dom options and components but still not getting a proper solution
My App page structure is something like as below
App.js
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import HomePage from './Pages/HomePage.js'
import ListingPage from './Pages/ListingPage.js'
import DetailPage from './Pages/DetailPage.js'
render() {
return (
<BrowserRouter>
<Switch>
<Route path="/home" component={HomePage} />
<Route path="/list" component={ListingPage} />
<Route path="/detail" component={DetailPage} />
</Switch>
</BrowserRouter>
)
}
HomePage.js
import React, { Component } from 'react';
class HomePage extends Component {
render() {
return (
<div>
//Showing list of different table and pages
</div>
);
}
}
export default HomePage;
ListingPage.js
import React, { Component } from 'react';
class ListingPage extends Component {
componentDidMount(){
this.getAllRecords()
}
getAllRecords = () => {}
goToDetailRecord = () => {
this.props.history.push('detail')
}
render() {
return (
<div>
//Table list data
</div>
);
}
}
export default ListingPage;
DetailPage.js
import React, { Component } from 'react';
class DetailPage extends Component {
goBack = () => {
this.props.history.goBack();
}
render() {
return (
<div>
//Shows table data detail value
</div>
);
}
}
export default DetailPage;
Is there anything I can implement to resolve the issue. Thanks in advance.
You can either useMemo or the react 16 and older version of that to store a cache to prevent it doing so on re renders or save the value in state using redux and not call the function if the field isn't empty. Also if component did mount works like useEffect I think by you having it there it will call that function on each re render.