React 中分页页面的唯一 url

Val*_*ner 5 javascript url pagination reactjs

我需要分页页面的唯一网址。

当我单击分页中的页数时 - url 已更改,例如.../album/1/page/(number of selected page) - 没问题,但是当我尝试复制我的 url 并将其粘贴为( .../album/1/page/2),我的应用程序仍然加载第一页。

我需要机会从 url 加载任何页面。

这是我此刻的路线:

<Route path="album/:id" component={AlbumsShow}>
    <Route path="page/:newPage" component={AlbumsShow} />
</Route>
Run Code Online (Sandbox Code Playgroud)

这是组件:

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);
Run Code Online (Sandbox Code Playgroud)

Prz*_*ski 2

请使用路由属性中的页码,即将:newPage其映射mapStateToPropspageNumber(可选)。

function mapStateToProps(state, ownProps) {
  const currentAlbumId = parseInt(ownProps.params.id, 10);
  const pageNumber = parseInt(ownProps.params.newPage, 10);

  return {
      pageNumber,
      images: state.main.albums.filter(album => album.id === currentAlbumId)[0].images,
  };
}
Run Code Online (Sandbox Code Playgroud)

然后,不要将页码存储在组件的状态中,而是将其作为道具使用。

<Pager current={this.props.pageNumber} ... />
Run Code Online (Sandbox Code Playgroud)

当处理到下一页的转换时,只需通知您的路由器,不要自行存储状态。您的组件将使用新的 pageNumber 值重新呈现。

handlePageChanged(newPage) {
    const { id } = this.props.params
    const { pageNumber } = this.props

    const target = `/albums/${id}/page/${pageNumber + 1}`

    browserHistory.push({
        pathname: target,
    });
}
Run Code Online (Sandbox Code Playgroud)

一路向下,改变出现的this.state.currenttothis.props.pageNumber就可以了。

URL 现在成为唯一的事实来源


注意:您可以更改路由参数以更好地匹配其含义:

<Route path="album/:albumId" component={AlbumsShow}>
    <Route path="page/:pageIndex" component={AlbumsShow} />
</Route>
Run Code Online (Sandbox Code Playgroud)

之后,您只需使用这些 id 而无需映射:

const { albumId, pageIndex } = this.props.params
Run Code Online (Sandbox Code Playgroud)