Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { useEffect, useState } from "react";
- import ReactPaginate from "react-paginate";
- import { IComment } from "../@types";
- //const items = [...Array(33).keys()];
- const Items = (currentItems:IComment[] | []) => {
- return (
- <div className="items">
- {
- currentItems && currentItems.map((item) => (
- <div>
- <h3></h3>
- </div>
- ))
- }
- </div>
- );
- }
- const PaginatedItems = ( items:IComment[] | [], itemsPerPage:number ) => {
- // We start with an empty list of items.
- const [currentItems, setCurrentItems] = useState<(IComment| null)[]>([]);
- const [pageCount, setPageCount] = useState<number>(0);
- // Here we use item offsets; we could also use page offsets
- // following the API or data you're working with.
- const [itemOffset, setItemOffset] = useState<number>(0);
- useEffect(() => {
- // Fetch items from another resources.
- const endOffset = itemOffset + itemsPerPage;
- console.log(`Loading items from ${itemOffset} to ${endOffset}`);
- setCurrentItems(items.slice(itemOffset, endOffset));
- setPageCount(Math.ceil(items.length / itemsPerPage));
- }, [itemOffset, itemsPerPage]);
- // Invoke when user click to request another page.
- const handlePageClick = (event:React.ChangeEvent<HTMLInputElement>) => {
- const newOffset = event.selected * itemsPerPage % items.length;
- console.log(`User requested page number ${event.selected}, which is offset ${newOffset}`);
- setItemOffset(newOffset);
- };
- return (
- <>
- <Items currentItems={currentItems} />
- <ReactPaginate
- nextLabel="next >"
- onPageChange={handlePageClick}
- pageRangeDisplayed={3}
- marginPagesDisplayed={2}
- pageCount={pageCount}
- previousLabel="< previous"
- 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}
- />
- </>
- );
- }
- export default PaginatedItems;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement