【发布时间】:2019-09-12 14:18:37
【问题描述】:
我的分页做了一些奇怪的事情。我用react-pager。
当我点击第一页时 - 我没有任何项目(但我注意到了),当我点击第二页时,我有 4 个项目,当我点击第三页时,我的项目刚刚添加到来自第二页。我的意思是在第三页上我有 8 个旧 + 新项目,而不是 4 个像我注意到的新项目。 我认为切片有问题。
组件如下:
import React, {Component} from 'react';
import {connect} from 'react-redux';
import Pager from 'react-pager';
class AlbumsShow extends Component {
constructor(props) {
super(props);
this.renderImage = this.renderImage.bind(this);
this.handlePageChanged = this.handlePageChanged.bind(this);
this.state = {
total: 3,
current: 1,
visiblePage: 2,
};
}
handlePageChanged(newPage) {
this.setState({ current : newPage });
}
renderImage() {
return this.props.images.slice((this.state.current-1), this.state.current * 4).map(image => (
<li key={image.id}>
<img className="album_img" alt="job" src={image.img} />
<p className="album_title">Test</p>
</li>
));
}
render() {
return (
<div>
<div>
{this.renderImage()}
</div>
<Pager
total={this.state.total}
current={this.state.current}
visiblePages={this.state.visiblePage}
titles={{ first: '<|', last: '>|' }}
className="pagination-sm pull-right"
onPageChanged={this.handlePageChanged}
/>
</div>
);
}
}
function mapStateToProps(state, ownProps) {
const currentAlbumId = parseInt(ownProps.params.id, 10);
return {
images: state.main.albums.filter(album => album.id === currentAlbumId)[0].images,
};
}
export default connect(mapStateToProps)(AlbumsShow);
此外,如果我将有更多 10 个项目,我的分页应该是可见的,如果少于 10 - null,但我的条件也不起作用。例如:
{this.renderImage() > 10 ?
<Pager
total={this.state.total}
current={this.state.current}
visiblePages={this.state.visiblePage}
titles={{ first: '<|', last: '>|' }}
className="pagination-sm pull-right"
onPageChanged={this.handlePageChanged}
/>: null }
【问题讨论】:
标签: reactjs pagination