-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a68d8b0
commit 6519374
Showing
7 changed files
with
54 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const ITEMS_PER_PAGE = 20; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useState } from 'react'; | ||
|
||
interface PaginationResult<T> { | ||
currentItems: T[]; | ||
currentPage: number; | ||
paginate: (pageNumber: number) => void; | ||
} | ||
|
||
/** | ||
* A custom hook for handling pagination. | ||
* | ||
* @param {T[]} items - The array of items to paginate. | ||
* @param {number} itemsPerPage - The maximum number of items per page. | ||
* | ||
* @returns {PaginationResult<T>} An object with the following properties: | ||
* - currentItems: The items for the current page. | ||
* - currentPage: The current page number. | ||
* - paginate: A function to change the current page. | ||
*/ | ||
export function usePagination<T>( | ||
items: T[], | ||
itemsPerPage: number | ||
): PaginationResult<T> { | ||
const [currentPage, setCurrentPage] = useState(1); | ||
|
||
const indexOfLastItem = currentPage * itemsPerPage; | ||
const indexOfFirstItem = indexOfLastItem - itemsPerPage; | ||
|
||
const currentItems = items.slice(indexOfFirstItem, indexOfLastItem); | ||
|
||
const paginate = (pageNumber: number) => setCurrentPage(pageNumber); | ||
|
||
return { currentItems, currentPage, paginate }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters