ReactPaginate Top and Bottom Paginator

300 Views Asked by At

Pagination item:

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;

So basically i have two paginators: top and bottom. The problem is that when i click on the paginator in the bottom state doesn't change on the top paginator.

Tried to put forcePage={itemOffset} but it works weird...

1

There are 1 best solutions below

1
On

From https://www.npmjs.com/package/react-paginate:

forcePage - Number - To override selected page with parent prop. Use this if you want to control the page from your app state.

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