
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
react-ts-pagination
Advanced tools
A ReactJS Component that creates pagination in a declarative and simple way
A React.ts Component to render pagination in a simple and Declarative way.
By installing the package you'll have this default pagination look, but you can easilly overwrite it using your own classes and styles.
/styles.css'"`. else you'll have to style everything using your own classes or style.
npm install @types/react-ts-pagination .
usePagination
, useServerPagination
that take cares of all the boilerplate for handling pagination states all in one line.Install react-ts-pagination
with npm:
npm install react-ts-pagination
with yarn:
yarn add react-ts-pagination
import { Pagination, usePagination } from 'react-ts-pagination
';
import 'react-ts-pagination
/styles.css';
function App() {
const { currentPageNumber, pageItems, numberOfPages, handlePageChange } = usePagination({
items,
itemsPerPage: 8,
});
return (
<div className='App'>
<Table>
{pageItems.map((page) => (
<tr key={page.id}>
<td>{page.id}</td>
<td>{page.first_name}</td>
<td>{page.last_name}</td>
<td>{page.email}</td>
<td>{page.phone}</td>
</tr>
))}
</Table>
<Pagination
currentPageNumber={currentPageNumber}
numberOfPages={numberOfPages}
onPageChange={handlePageChange}
/>
</div>
);
}
import { Pagination, useServerPagination } from 'react-ts-pagination
';
import 'react-ts-pagination
/styles.css';
function App() {
const {
pageItems,
isLoading,
currentPageNumber,
handlePageChange
} = useServerPagination<Repo[]>({
url: 'https://api.github.com/orgs/GSG-G11/repos',
searchParams: { page: 'page', perPage: 'per_page' },
itemsPerPage: 5,
numberOfPages: 12,
});
return (
<div className='App'>
{isLoading ? (
<Skeleton />
) : (
<Table tableHeaders={tableHeaders}>
{pageItems?.map(({ id, name, description, owner, visibility }) => (
<tr key={id}>
<td>{id}</td>
<td>{name}</td>
<td>{owner.login}</td>
<td>{description?.slice(0, 20)}</td>
<td>{visibility}</td>
</tr>
))}
</Table>
)}
<Pagination
currentPageNumber={currentPageNumber}
numberOfPages={numberOfPages}
onPageChange={handlePageChange}
/>
</div>
);
}
import { Pagination } from 'react-ts-pagination
';
import 'react-ts-pagination
/styles.css';
const ITEMS_PER_PAGE = 10;
const numberOfPages = Math.ceil(items.length / ITEMS_PER_PAGE);
function App() {
const [pageItems, setPageItems] = useState < typeof items > [];
const currentPageNumber = useRef(1);
const handlePageChange = (pageNumber: number, pageRef: HTMLSpanElement | undefined) => {
const FIRST_PAGE_NUMBER = 1;
const LAST_PAGE_NUMBER = numberOfPages;
const isFirstPage = pageNumber + 1 === FIRST_PAGE_NUMBER;
const isLastPage = pageNumber - 1 === LAST_PAGE_NUMBER;
if (isLastPage || isFirstPage) return;
const start = (pageNumber - 1) * ITEMS_PER_PAGE;
const end = pageNumber * ITEMS_PER_PAGE;
currentPageNumber.current = pageNumber;
setPageItems(items.slice(start, end));
};
useEffect(() => {
const start = (currentPageNumber.current - 1) * ITEMS_PER_PAGE;
const end = currentPageNumber.current * ITEMS_PER_PAGE;
setPageItems(items.slice(start, end));
}, []);
return (
<div className='App'>
<Table>
{pageItems.map((page) => (
<tr key={page.id}>
<td>{page.id}</td>
<td>{page.first_name}</td>
<td>{page.last_name}</td>
<td>{page.email}</td>
<td>{page.phone}</td>
</tr>
))}
</Table>
<Pagination
currentPageNumber={currentPageNumber.current}
numberOfPages={numberOfPages}
onPageChange={handlePageChange}
/>
</div>
);
}
usePagination hook:
Name | Type | Description |
---|---|---|
items | Array | Required: The Array that you want the paginate on. |
initialPageNumber | Number | Optional: The initial page selected. Default is 1 |
ItemsPerPage | Number | Optional: the number of items to display on each page. Default is 10 |
Name | Type | Description |
---|---|---|
pageItems | Array | The current items state, for the current page number selected |
currentPageNumber | Number | The page number state |
numberOfPages | Number | The computed number of total pages that should be rendered, depending on the passed items array length |
handlePageChange | Funtion | the handler function to handle changing pages, it expects pageNumber and pageRef to be passed as parameters |
useServerPagination hook:
Name | Type | Description |
---|---|---|
url | string | Required: The endpoint for your Api (without the search quries). |
searchParams | Object | Required: an object that contains The search queries for your Api. page: a string that tells the server which page number you want to retrieve. perPage: a string that tells the server how many items to retrieve for each page}` |
initialPageNumber | Number | Optional: The initial page selected. Default is 1 |
ItemsPerPage | Number | Required: The number of items to display on each page. Default is 10 |
Name | Type | Description |
---|---|---|
isLoading | Boolean | A boolean that presents the state of of the request |
isError | Boolean | A boolean that indicates if error accured or not while fetching the page |
error | object | A standard error object that changes for each page request |
currentPageNumber | Number | The page number state |
handlePageChange | Funtion | the handler function to handle changing pages, it expects pageNumber and pageRef to be passed as parameters |
Pagination Component:
Name | Type | Description |
---|---|---|
currentPageNumber | Number | Required: The current page number state. You can either get it from usePagination hook or you can pass you own currentPageNumber state. |
numberOfPages | Number | Required: The number of total pages that should be generated. You can either get it from usePagination hook or you can pass you own numberOfPages state. |
onPageChange | Function: (page,pageRef)=>{} | Required: the handler function to handle changing pages, it gets passed the currentPageNumber and the the dom reference for current page. |
nextLabel | String or Refrence to A Component | Optional: The next button text label. Default is : ❯ |
nextBtnClass | String | Optional: A class name to apply to the next button. Default is btn |
prevLabel | String or Refrence to A Component | Optional: The prev button text label. Default is : ❮ |
prevBtnClass | String | Optional: A class name to apply to the prev button. Default is btn |
pageStyle | Object | Optional: The defualt page style object with color and backgroundColor properties. Default is: undefined |
activePageSyle | Object | Optional: The acitve page style object with color and background propeties. Default is undefined |
pageClass | String | Optional: A class name to apply to each page. The default class is page |
activePageClass | String | Optional: A class name to to apply to the current acitve page or the page that being hovered. Default is active-page |
paginationContainerClass | String | Optional: A class name to apply to the parent container for the whole component. Default is pagination |
pagesContianerClass | String | Optional: A class name to apply to the direct parent of the pages. Default is pages |
To run the demo locally, clone the repository and move into it:
git clone git@github.com:amjed-98/react-ts-pagination
.git
cd react-ts-pagination
Install dependencies:
npm install | yarn
preview the Demo
npm run demo | yarn demo
Open your browser and go to http://127.0.0.1:5173/src/demo/index.html
Run the tests
npm run test | yarn test
Run the tests in the browser with nice UI presentation
npm run test:ui | yarn test:ui
FAQs
A ReactJS Component that creates pagination in a declarative and simple way
The npm package react-ts-pagination receives a total of 1 weekly downloads. As such, react-ts-pagination popularity was classified as not popular.
We found that react-ts-pagination demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.