-
How can I add pathname along with searchParams with nuqs? Example I want to add pathname: page with searchParams year=2024&type=export then click on a button to push to page(pathname). |
Beta Was this translation helpful? Give feedback.
Answered by
franky47
Jan 31, 2024
Replies: 1 comment 3 replies
-
The Your example would look like this: import { createSerializer, parseAsInteger, parseAsStringLiteral } from 'nuqs'
// Example of search params description, which can be reused in `useQueryStates`:
const searchParamsParsers = {
year: parseAsInteger,
type: parseAsStringLiteral(['export', ...] as const)
}
const serialize = createSerializer(searchParamsParsers)
function MyComponent() {
function onClick() {
const url = serialize('/page', {
year: 2024,
type: 'export'
})
// Note that you can't do shallow routing here (in the app router)
router.push(url)
}
return <button onClick={onClick}>...</button>
} |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
franky47
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
useQueryState(s)
hooks will only update the search params on the current page, but you can render a URL from state values usingcreateSerializer
, and feed it to the router.Your example would look like this: