【发布时间】:2017-10-26 04:48:01
【问题描述】:
我需要分页页面的唯一网址。
当我点击分页中的页数时 - url 改变了,比如 .../album/1/page/(number of selected page) - 没关系,但是当我尝试复制我的网址并将其粘贴为 (.../album/1/page/2),我的应用仍会加载第一页。
我需要机会从 url 加载任何页面。
这是我此刻的路线:
<Route path="album/:id" component={AlbumsShow}>
<Route path="page/:newPage" component={AlbumsShow} />
</Route>
这是组件:
import React, {Component} from 'react';
import {connect} from 'react-redux';
import Pager from 'react-pager';
import {browserHistory} from 'react-router';
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: 0,
visiblePage: 2,
};
}
handlePageChanged(newPage) {
this.setState({ current : newPage });
browserHistory.push({
pathname: (newPage+1),
});
}
renderImage() {
const imgs = this.props.images.length > 10 ?
this.props.images.slice(this.state.current * 10, (this.state.current + 1) * 10) :
this.props.images;
return imgs.map((image, page) => (
<li className="image_list" key={image.id}>
<img className="image_list_img" alt="item" src={image.img} />
</li>
));
}
render() {
return (
<div className="wrapper">
<ul>
{this.renderImage()}
</ul>
{this.props.images.length > 10 ?
<Pager
total={this.state.total}
current={this.state.current}
visiblePages={this.state.visiblePage}
titles={{ first: 'First page', last: 'Last page' }}
onPageChanged={this.handlePageChanged}
/> : null
}
</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);
【问题讨论】:
标签: javascript reactjs url pagination