
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
@aboutbits/react-pagination
Advanced tools
Query hooks for React with first-class support for TypeScript! Writing and reading the query, attached to the browser URL or in-memory, made easy!
Install the package:
npm install @aboutbits/react-pagination
There are a variety of entry points from which to import the hooks useQuery
, usePagination
and useQueryAndPagination
:
@aboutbits/react-pagination/next-router
@aboutbits/react-pagination/next-router/zod
@aboutbits/react-pagination/react-router
@aboutbits/react-pagination/react-router/zod
@aboutbits/react-pagination/in-memory
@aboutbits/react-pagination/in-memory/zod
The hooks exported from @aboutbits/react-pagination/*/zod
are more convenient when using zod for the validation of the query.
useQueryAndPagination
merges the functionality of useQuery
and usePagination
. Changing the query resets the page, but changing the page does not reset the query.
Some examples follow, but we recommend having a look at the type definitions for more details about the API.
import { Query } from '@aboutbits/react-pagination/dist/engine'
import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/routers/nextRouter'
const users = ['Alex', 'Simon', 'Natan', 'Nadia', 'Moritz', 'Marie']
const parseSearch = (query: Query) => {
for (const [key, value] of Object.entries(query)) {
if (key === 'search' && !Array.isArray(value)) {
return { search: value }
}
}
return {}
}
export function UserList() {
const { page, size, query, setQuery, setPage, resetQuery } =
useQueryAndPagination(parseSearch, { search: '' })
return (
<div>
<input
value={query.search}
onChange={(event) => setQuery({ search: event.target.value })}
/>
<button onClick={() => resetQuery()}>Clear Input</button>
<select
value={page}
onChange={(event) => setPage(parseInt(event.target.value))}
>
<option value="0">First Page</option>
<option value="1">Second Page</option>
</select>
<ul>
{users
.filter((user) =>
user.toLowerCase().startsWith(query.search.toLowerCase()),
)
.slice(page * size, (page + 1) * size)
.map((user) => (
<li key={user}>{user}</li>
))}
</ul>
</div>
)
}
Notice that we let zod take a string and then coerce it to a number. If the coercion fails, we do not stop the whole parsing, but default the property to undefined
.
This allows us to still use departmentId
if it is defined while userId
is not and vice versa.
import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/zod/routers/nextRouter'
import { z } from 'zod'
export function Component() {
const { page, size, query, setQuery, setPage, resetQuery } =
useQueryAndPagination(
z.object({
departmentId: z
.string()
.pipe(z.coerce.number().optional())
.catch(undefined),
userId: z.string().pipe(z.coerce.number().optional()).catch(undefined),
}),
)
// ... do something
}
import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/zod/routers/reactRouter'
import { z } from 'zod'
const userSchema = z.object({
name: z.string(),
// The input to the parser is going to be a string.
// We try to convert it to a number and default to undefined if the parsing fails.
// This continues the parsing of the remaining query.
// Another possibility would be to not catch errors, which would cancel the entire parsing
// if "age" cannot be converted to a number.
age: z.string().pipe(z.coerce.number().optional()).catch(undefined),
})
const users = [
{ name: 'Alex', age: 10 },
{ name: 'Simon', age: 24 },
{ name: 'Natan', age: 88 },
{ name: 'Nadia', age: 42 },
{ name: 'Moritz', age: 35 },
{ name: 'Marie', age: 17 },
]
export function UserList() {
const { page, size, query, setQuery, setPage, resetQuery } =
useQueryAndPagination:
userSchema,
{ name: '', age: 0 },
{
page: 0,
size: 4,
},
)
return (
<div>
<div>
Name:
<input
value={query.name}
onChange={(event) => setQuery({ name: event.target.value })}
/>
</div>
<div>
Minimum age:
<input
value={query.age}
onChange={(event) => {
const value = event.target.value
const parsed = parseInt(value)
if (!isNaN(parsed)) {
setQuery({ age: parsed })
}
}}
/>
</div>
<button onClick={() => resetQuery()}>Clear Input</button>
<select
value={page}
onChange={(event) => setPage(parseInt(event.target.value))}
>
<option value="0">First Page</option>
<option value="1">Second Page</option>
</select>
<ul>
{users
.filter(
(user) =>
user.name.toLowerCase().startsWith(query.name.toLowerCase()) &&
user.age >= query.age,
)
.slice(page * size, (page + 1) * size)
.map((user) => (
<li key={user.name}>{user.name}</li>
))}
</ul>
</div>
)
}
To publish the package commit all changes and push them to main. Then run one of the following commands locally:
npm version patch
npm version minor
npm version major
AboutBits is a company based in South Tyrol, Italy. You can find more information about us on our website.
For support, please contact info@aboutbits.it.
The MIT License (MIT). Please see the license file for more information.
FAQs
Pagination hooks for React
The npm package @aboutbits/react-pagination receives a total of 500 weekly downloads. As such, @aboutbits/react-pagination popularity was classified as not popular.
We found that @aboutbits/react-pagination demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 9 open source maintainers 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
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.