css ReactPaginate顶部和底部分页程序

8ljdwjyq  于 2022-12-15  发布在  React
关注(0)|答案(1)|浏览(127)

分页项目:

const CarsSection = () => {
  const itemsPerPage = 8;
  const [itemOffset, setItemOffset] = useState(0);
  const endOffset = itemOffset + itemsPerPage;
  const currentItems = carsData.slice(itemOffset, endOffset);
  const pageCount = Math.ceil(carsData.length / itemsPerPage);

  const handlePageClick = (event) => {
    const newOffset = (event.selected * itemsPerPage) % carsData.length;
    setItemOffset(newOffset);
  };

  return (
    <>
        <PaginateContainer>
          <ReactPaginate
            nextLabel=">"
            onPageChange={handlePageClick}
            pageRangeDisplayed={3}
            marginPagesDisplayed={2}
            pageCount={pageCount}
            previousLabel="<"
            pageClassName="page-item"
            pageLinkClassName="page-link"
            previousClassName="page-item"
            previousLinkClassName="page-link"
            nextClassName="page-item"
            nextLinkClassName="page-link"
            breakLabel="..."
            breakClassName="page-item"
            breakLinkClassName="page-link"
            containerClassName="pagination"
            activeClassName="active"
            renderOnZeroPageCount={null}
          />
        </PaginateContainer>
        <CarsContainer>
          <Items currentItems={currentItems} />
        </CarsContainer>

//Same <ReactPaginate /> (code is too big couldn't put it down)//

    </>
  );
};

export default CarsSection;

基本上我有两个分页器:顶部和底部。问题是,当我点击底部状态的分页器时,顶部的分页器不会改变。
尝试放置forcePage={itemOffset},但效果不佳...

ccrfmcuu

ccrfmcuu1#

https://www.npmjs.com/package/react-paginate开始:
forcePage - Number -使用父属性覆盖选定页面。如果要从应用状态控制页面,请使用此选项。

const [page, setPage] = useState(0);
<ReactPaginate
  forcePage={page}
  onPageChange={page => setPage(page)}
  ...
/>

相关问题