I'm implementing pagination
functionality by semantic-ui-react
.
I can implement pagination component itself, but can't implement onPageChange
to set activePage
and control number of pages displayed.
I use react for client side functionality.
Also I use semantic-ui-react for css framework.
All contents of array is listed on single page now.
But I want to implement pagination and limit to display 5 contents on single page.
I somehow understand I need to use onPageChange
, but don't know how to implement to achieve that goal.
import React from 'react';
import {Link} from 'react-router-dom';
import {Grid, Segment, Container, Header, Pagination} from 'semantic-ui-react';
import axios from 'axios';
import {Article} from '../articleData';
interface ArticleState {
articles: Article[];
}
class List extends React.Component<{}, ArticleState> {
constructor(props: {}) {
super(props);
this.state = {
articles: [],
};
this.serverRequest = this.serverRequest.bind(this);
this.btnClick = this.btnClick.bind(this);
}
serverRequest() {
axios
.get('/api/articles')
.then(response => {
this.setState({articles: response.data});
})
.catch(response => console.log('ERROR!! occurred in Backend.'));
}
btnClick(event: React.MouseEvent<HTMLAnchorElement>, data: object) {
}
componentDidMount() {
this.setState({articles: []});
this.serverRequest();
}
render() {
return (
<Container style={{marginTop: '7em'}} text>
<Grid columns={1} divided="vertically">
<Grid.Row>
{(this.state.articles || []).map(function(articleData, i) {
return (
<Grid.Column>
<Segment>
<Header as="h1">{articleData.title}</Header>
<p>{articleData.content}</p>
<Link to={`/detail/${articleData.id}`}>
continue reading
</Link>
</Segment>
</Grid.Column>
);
})}
</Grid.Row>
</Grid>
<Pagination
defaultActivePage={5}
totalPages={Math.floor(this.state.articles.length / 2) + 1}
//onPageChange={this.btnClick}
/>
</Container>
);
}
}
export default List;
I expect the pagination functionality to limit number of displayed content to 5 on single page.
But actually I don't know how to implement this functionality.