Skip to content

rezan83/Chakra-PaginatorX

Repository files navigation

PaginatorX

Pagination for Chakra-ui made easy

- Depends on "@chakra-ui/icons" and "@chakra-ui/react"

- How to use:

  1. first import Paginator component and usePaginator hook

import Paginator, { usePaginator } from 'paginatorx-chakra';
  1. better to prepare your data array using useEffect or custom hook or any other effect fetch library like react query, and your data type in case you are using typescript

import { TyourDataType } from 'your types folder';
const yourData: TyourDataType[] = useYourUseFetch;
  1. feed usePaginator hook with data arry and count of items per page, the generic might not be needed because usePaginator will infer it from your data type

const { pagesCount, selectedPage, setSelectedPage, pages } =
  usePaginator < TyourDataType > (yourData, countPerPage);
  1. consume the returned pages array (array of arrays, each one represent data in a page) with selectedPage as index

<div>
  {!!pages.length &&
    pages[selectedPage].map((item: TyourDataType) => {
      return <Item key={item.id} item={item} />;
    })}
</div>
  1. Paginator component needs pagesCount, selectedPage and setSelectedPage, all are returned from usePaginator. Notice if you have your own logic to calculate these props then you don't need usePaginator

{
  pages.length > 1 && (
    <Paginator
      pagesCount={pagesCount}
      selectedPage={selectedPage}
      setSelectedPage={setSelectedPage}
    />
  );
}

About

pagination for chakra-ui made easy

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published