我的分页正在做一些奇怪的事情。我使用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 - 空,但我的条件不工作压力太大。例如:
{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 }
它可以帮助和我的项目不添加到对方,但我的第一页仍然是空的。 –
分页工作,谢谢。 我还有一个关于条件的问题: 当我把你的编辑粘贴到我的代码中时,我的物品消失了,但控制台很清晰。 –
你的意思是当你有少于10张图片时,你会显示0张图片吗?如果是这样,这是正常的,因为如果条件为假,则返回null。我将编辑我的答案,告诉你如何去做。 –