pagination
Certainly! Below is a simple TypeScript React component that provides a paginated list of items.
import React, { useState } from 'react';
type PaginationProps = {
items: any[]; // Replace `any` with your specific item type
itemsPerPage: number;
};
const Pagination: React.FC<PaginationProps> = ({ items, itemsPerPage }) => {
const [currentPage, setCurrentPage] = useState(1);
const maxPage = Math.ceil(items.length / itemsPerPage);
const begin = (currentPage - 1) * itemsPerPage;
const end = begin + itemsPerPage;
const currentItems = items.slice(begin, end);
const goToNextPage = () => setCurrentPage((page) => Math.min(page + 1, maxPage));
const goToPreviousPage = () => setCurrentPage((page) => Math.max(page - 1, 1));
const goToPage = (page: number) => setCurrentPage(page);
return (
<div>
{/* Item list */}
{currentItems.map((item, index) => (
<div key={index}>{item}</div>
))}
{/* Pagination controls */}
<div>
<button onClick={goToPreviousPage} disabled={currentPage === 1}>
Previous
</button>
{[...Array(maxPage).keys()].map((pageIndex) => (
<button
key={pageIndex}
onClick={() => goToPage(pageIndex + 1)}
disabled={pageIndex + 1 === currentPage}
>
{pageIndex + 1}
</button>
))}
<button onClick={goToNextPage} disabled={currentPage === maxPage}>
Next
</button>
</div>
</div>
);
};
export default Pagination;
To use this component, you would pass in an array of items and specify how many items you want per page:
const App: React.FC = () => {
const items = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6', 'Item 7']; // Replace with your items
return <Pagination items={items} itemsPerPage={2} />;
};
export default App;
The Pagination
component calculates the number of pages based on the total number of items and items per page. It then renders the current page's items and displays a list of page numbers as well as Previous and Next buttons to navigate through the pages.
Let me know if you'd like to proceed with the step-by-step explanation!
Alright, let's go through the step-by-step explanation of the pagination component implementation:
Define the Component Props:
- We start by defining the TypeScript type for the props that the
Pagination
component will accept. ThePaginationProps
type includes anitems
array for the list of items we want to paginate and anitemsPerPage
number to define how many items to show per page.
- We start by defining the TypeScript type for the props that the
Component State:
- Within the
Pagination
component, we use theuseState
hook to create acurrentPage
state variable initialized to1
. This state will track the current page number.
- Within the
Calculate Pagination Details:
- We calculate the maximum number of pages (
maxPage
) as the ceiling value of the total number of items divided by the number of items per page. This is done usingMath.ceil(items.length / itemsPerPage)
.
- We calculate the maximum number of pages (
Determining the Items to Display:
- To find out which items to display on the current page, we calculate the starting index (
begin
) and the ending index (end
) of the items for the current page. This is done with basic arithmetic based on thecurrentPage
anditemsPerPage
. - We then slice the
items
array frombegin
toend
to get thecurrentItems
, which are the items to display on the current page.
- To find out which items to display on the current page, we calculate the starting index (
Navigation Functions:
- We define three functions to handle pagination navigation:
goToNextPage
: Increments thecurrentPage
state, capped atmaxPage
.goToPreviousPage
: Decrements thecurrentPage
state, with a minimum value of1
.goToPage
: Sets thecurrentPage
state to a specific page number passed to it.
- We define three functions to handle pagination navigation:
Rendering the Paginated Items:
- We map over
currentItems
to render the items that should be visible on the current page. Each item is wrapped in adiv
and given akey
prop for React to track each element efficiently.
- We map over
Rendering Pagination Controls:
- We render a "Previous" button that calls
goToPreviousPage
when clicked and is disabled ifcurrentPage
is1
. - We then use an array created by
[...Array(maxPage).keys()]
to map through all the page numbers, rendering a button for each page. These buttons callgoToPage
with the respective page number and are disabled if their page number is the current page. - Finally, we render a "Next" button that calls
goToNextPage
when clicked and is disabled ifcurrentPage
is equal tomaxPage
.
- We render a "Previous" button that calls
Styling and Interaction:
- Basic inline styles or CSS classes could be added to style the pagination controls and the list of items. Additionally, one might want to add more sophisticated interactions or animations to the pagination transitions.
Usage:
- The
Pagination
component is used in theApp
component or any other parent component by passing theitems
array and theitemsPerPage
value as props.
- The
This simple pagination component provides the basic functionality required to navigate through a list of items. It can be expanded with additional features such as page range displays, ellipsis for hidden pages, and so on, depending on the needs of your application.