react pagination分页使用
在React中使用分页组件通常需要完成以下步骤:
1. 安装所需的分页库,例如`react-paginate`或`react-bootstrap`中的分页组件。
npm install react-paginate
2. 导入分页组件。
jsx
import ReactPaginate from 'react-paginate';bootstrap 5
3. 在组件的状态中添加有关分页的信息,例如当前页码和每页显示的项数。
jsx
state = {
  currentPage: 1,
  itemsPerPage: 10,
  其他状态属性...
}
4. 创建一个处理页码更改的方法,用于更新当前页码。
jsx
handlePageChange = (selectedPage) => {
  this.setState({
    currentPage: selectedPage.selected
  });
}
5. 在组件的`render()`方法中使用分页组件并配置相关属性。
jsx
render() {
  计算当前页的数据
  const offset = (this.state.currentPage - 1) * this.state.itemsPerPage;
  const currentPageData = data.slice(offset, offset + this.state.itemsPerPage);
  return (
    <div>
      {/* 显示当前页的数据 */}
      {currentPageData.map((item, index) => (
        <div key={index}>{item}</div>
      ))}
      {/* 显示分页组件 */}
      <ReactPaginate
        previousLabel={'Prev'}
        nextLabel={'Next'}
        pageCount={il(data.length / this.state.itemsPerPage)}
        marginPagesDisplayed={2}
        pageRangeDisplayed={5}
        onPageChange={this.handlePageChange}
        containerClassName={'pagination'}
        activeClassName={'active'}
      />
    </div>
  );
}
这些是使用`react-paginate`库进行分页的基本步骤。你也可以根据你的需求进行其他配置和自定义。