【发布时间】:2021-04-15 23:10:30
【问题描述】:
我正在尝试使用 antd 分页对来自分页 api 的帖子列表进行分页,下面的代码是我想要做的,但我不知道如何让它工作。
我在 React 中使用 Antd Pagination,在 Django Rest Api 中使用 LimitOffsetPagination。
class Archive extends Component {
constructor(props) {
super(props);
this.state = {
loading: true,
posts: [],
offset:0
};
this.handleChange = this.handleChange.bind(this);
}
handleChange () {
this.offset = this.offset + 4
};
async componentDidMount() {
axios.get('/api/Post?limit=4&offset='+ offset)
.then((response) => {
this.setState({
posts: response.data.results,
loading: false
})
})
}
render() {
return (
<React.Fragment>
{ this.state.posts.map((post) => {
return (
<div>
<a href={`/Post/${post.id}`}>
<img src={post.image} width="500" height="275"/>
</a>
<div>
<div class="">
<Link to={`/Post/${post.id}`}>
{" "}
{post.title}{" "}
</Link>
</div>
</div>
</div>
);
})}
<Pagination
defaultCurrent={2}
defaultPageSize={4}
onChange={this.handleChange}
total={300}
/>
</React.Fragment>
)
}
}
【问题讨论】:
标签: reactjs django-rest-framework pagination