A custom React hook that helps in generating a pagination range based on the provided options

usePagination Hook

The usePagination hook is a custom React hook that helps in generating a pagination range based on the provided options. It takes the following parameters:

interface PaginationOptions {
  totalCount: number;
  pageSize: number;
  siblingCount?: number;
  currentPage: number;
}
  • totalCount: The total number of items to be paginated.
  • pageSize: The number of items per page.
  • siblingCount (optional): The number of siblings to display on each side of the current page. Defaults to 1.
  • currentPage: The current page number.

The usePagination hook returns a pagination range array, which consists of either page numbers or “DOTS” placeholders. The generated pagination range is suitable for rendering pagination links or buttons.

import { usePagination } from "@src/hooks";

const MyComponent: React.FC = () => {
  const paginationRange = usePagination({
    totalCount: 100,
    pageSize: 10,
    siblingCount: 1,
    currentPage: 3,
  });

  return (
    <div>
      {paginationRange.map((pageNumber, index) => (
        <span key={index}>{pageNumber}</span>
      ))}
    </div>
  );
};

In the above example, paginationRange will contain an array representing the pagination links based on the provided options.

Pagination Component

The Pagination component is a reusable React component that utilizes the usePagination hook to render a pagination UI. It provides a set of navigation controls and displays the pagination range.

Props:

interface Props {
  onPageChange: (page: number) => void;
  totalCount: number;
  siblingCount?: number;
  currentPage: number;
  pageSize: number;
  className?: string;
}
  • onPageChange: A callback function that is invoked when a page is selected. It receives the selected page number as a parameter.
  • totalCount: The total number of items to be paginated.
  • siblingCount (optional): The number of siblings to display on each side of the current page. Defaults to 1.
  • currentPage: The current page number.
  • pageSize: The number of items per page.
  • className (optional): Additional CSS classes to be applied to the component.

Example usage:

import Pagination from "@src/components/Pagination";

const MyComponent: React.FC = () => {
  const handlePageChange = (page: number) => {
    // Perform logic to update the current page
  };

  return (
    <div>
      <Pagination
        onPageChange={handlePageChange}
        totalCount={100}
        siblingCount={1}
        currentPage={3}
        pageSize={10}
        className="my-pagination"
      />
    </div>
  );
};

In the above example, the Pagination component will render pagination links based on the provided props. When a page is selected, the handlePageChange callback will be triggered, allowing you to update the current page state.

Note: The Pagination component relies on the usePagination hook to generate the pagination range. Make sure to import the usePagination hook and provide it as a dependency in your project.

GitHub

View Github

Related Posts

Recent Posts

ഇടുക്കിയിലെ മലയോര മേഖലകളിൽ രാത്രിയാത്ര നിരോധിച്ചു. രാത്രി ഏഴു മുതൽ രാവിലെ ആറു വരെയാണ് നിരോധനം

ഏന്തയാർ ഈസ്റ്റിൽ പ്രളയത്തിൽ തകർന്ന പാലത്തിന് പകരം പുതിയ പാലം നിർമ്മിക്കുവാൻ താത്ക്കാലിക പാലം പൊളിച്ച് നീക്കി

Explore the Investment Opportunities: A Comprehensive Guide to Different Types of Mutual Funds

Title: Understanding Mutual Funds: A Beginner's Guide to Investing

തീവ്രമഴ മുന്നറിയിപ്പിന്റെ പശ്ചാതലത്തിൽ സംസ്ഥാനം ജാഗ്രതയിൽ

250,000 അപേക്ഷകൾ വർദ്ധിച്ചതിനാൽ ട്രാൻസ്‌പോർട്ട് കമ്മീഷണർ പരിശോധന പുനരാരംഭിക്കും

ഏലക്കയിൽ കീടനാശിനി സാന്നിധ്യം; ആറര ലക്ഷത്തിലധികം ടിൻ അരവണ നശിപ്പിക്കാൻ ടെൻഡർ ക്ഷണിച്ച് ദേവസ്വം ബോർഡ്‌

ഭീമൻ പാറക്കഷണങ്ങൾ അടർന്ന് ദേശീയ പാതയിലേക്ക് വീഴുന്നത് പതിവാകുന്നു. കുട്ടിക്കാനത്തിനും മുണ്ടക്കയത്തിനുമിടയിൽ നിലനിൽക്കുന്നത് വൻ അപകട ഭീഷണി

ചക്രവാതച്ചുഴി:അതിശക്തമായ മഴ വരുന്നു

പ്ലസ് വൺ പ്രവേശനം. അക്ഷയയിൽ തിക്കി തിരക്കേണ്ട, നെറ്റിവിറ്റി/ജാതി തെളിയിക്കാൻ പത്താംതരം സർട്ടിഫിക്കറ്റ് മതി