分页项目:
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},但效果不佳...
1条答案
按热度按时间ccrfmcuu1#
从https://www.npmjs.com/package/react-paginate开始:
forcePage - Number -使用父属性覆盖选定页面。如果要从应用状态控制页面,请使用此选项。