-
Hi, I came across this package today and was wondering if it's possible to use the For example, I want to ensure search params are validated on the Server: import { parseAsInteger } from 'next-usequerystate';
export default async function Page({
searchParams: { page },
}: {
searchParams: { page?: string };
}) {
const parsedPage = parseAsInteger(page).withDefault(1);
// ...
}; Client: 'use client';
import { parseAsInteger, useQueryState } from 'next-usequerystate';
const Component () => {
const [page] = useQueryState('page', parseAsInteger.withDefault(1));
// ...
}; Many thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
While there there is nothing server or client-specific about parsers, the fact that they're imported from
It would be possible I guess to export the parsers in a separate, non- Assuming we're having no import issues, there would still be a bit of data massaging to get to the same value as what the hook returns: the default value is contained in the parser, but calls to Also, you'll probably want to define and configure your parser in a dedicated file that gets imported in both client and server files, to reduce duplication. This is what it could look like: // path/to/parsers.ts
import { parseAsInteger } from 'next-usequerystate/parsers'
export const pageParser = parseAsInteger.withDefault(1); Server: import { pageParser } from 'path/to/parsers';
export default async function Page({
searchParams: { page },
}: {
searchParams: { page?: string };
}) {
const parsedPage = pageParser.parse(page ?? '') ?? pageParser.defaultValue;
// ...
}; Client: 'use client';
import { useQueryState } from 'next-usequerystate';
import { pageParser } from 'path/to/parsers'
const Component () => {
const [page] = useQueryState('page', pageParser);
// ...
}; |
Beta Was this translation helpful? Give feedback.
While there there is nothing server or client-specific about parsers, the fact that they're imported from
next-usequerystate
-- which is a client-only import -- makes it forbidden to call functions on them:It would be possible I guess to export the parsers in a separate, non-
'use-client'
file, that can be imported on the server and the client (eg:import { parseAsInteger } from 'next-usequerystates/parsers'
).Assuming we're having no import issues, there would still be a bit of data massaging to get to the same value as what …