I want to render a table in React on a page. Table has contains a lot of rows so I want to apply pagination. Like when someone clicks on page 2 link(/open_alerts/?page=2), table fetches next rows while keeping the same page and just changing the data. But i am facing problem, i can not know in my AlertsTable component the value of page parameter. i saw something useParams hook but this is not allowed in class component. How can I achieve the desired thing?
Home page is at / and it contains link to /open_alerts/ page. App.js:
<Router>
<Switch>
<Route exact path="/">
<Link to="/open_alerts/">Open Alerts</Link>
<div className="Charts">
<AlertsChart id="alerts-chart"/>
<RegionsChart id="regions-chart"/>
</div>
</Route>
<Route path="/open_alerts/">
<Link to="/">Home page</Link>
<AlertsTable />
</Route>
</Switch>
</Router>
How do i know in AlertsTable component the page number so that i may use page number in my API call to server to get the paginated response?
alerts_table.js:
export class AlertsTable extends React.Component {
.
. // code here
.
render() {
return (
<div>
<h1>
Open alerts
</h1>
{ !this.state.isfetched ? <p>Fetching open alerts</p>:
<div>
<AlertsRenderer alerts={this.state.alerts}/>
<PaginatedButton pages={this.state.pages_count} />
</div> }
</div>
);
}
}
paginated_button.js:
export function PaginatedButton(props) {
return (
<Router>
{props.pages.map(page_no => (
<button>
<Link exact to={"/open_alerts/?page="+(parseInt(page_no)+1)}>{page_no+1}</Link>
</button>
))}
</Router>
)}
AlertsRenderer is just a functional component that gets alerts from props and displays using HTML table tags.