useQueryParams()
Type-safe query param handling for Next.js.
const { setParams, params } = useQueryParams(
{
str: "string",
num: "number",
pets: {
type: "string[]",
},
bool: "boolean",
withDefault: {
type: "number",
default: 42,
},
checky: {
type: "string[]",
default: ["1"],
},
},
);
type ParamOptionTypes =
| "string"
| "string[]"
| "number"
| "number[]"
| "boolean";
export interface UseQueryParamsOptions {
type?: "replace" | "push";
transitionOptions?: TransitionOptions;
}
Example:
const { setParams, params } = useQueryParams(
{
// [...]
},
{
type: "replace",
transitionOptions: {
scroll: true,
}
}
);
In order to make this work in SSR, you have to opt-out of SSG - see pages/_app.tsx
for an example on how to disable SSG.