Skip to content

Commit 66542bc

Browse files
author
devgioele
committed
add example without default query
1 parent 67d0dae commit 66542bc

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

readme.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,31 @@ export function UserList() {
8989
}
9090
```
9191

92+
### Example usage with Next.js, zod and no default query
93+
94+
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`.
95+
This allows us to still use `departmentId` if it is defined while `userId` is not and vice versa.
96+
97+
```tsx
98+
import { useQueryAndPagination } from '@aboutbits/react-pagination/dist/zod/routers/nextRouter'
99+
import { z } from 'zod'
100+
101+
export function Component() {
102+
const { page, size, query, setQuery, setPage, resetQuery } =
103+
useQueryAndPagination(
104+
z.object({
105+
departmentId: z
106+
.string()
107+
.pipe(z.coerce.number().optional())
108+
.catch(undefined),
109+
userId: z.string().pipe(z.coerce.number().optional()).catch(undefined),
110+
}),
111+
)
112+
113+
// ... do something
114+
}
115+
```
116+
92117
### Example usage with React Router and zod
93118

94119
```tsx

0 commit comments

Comments
 (0)