App router routing with searchParams
Unanswered
inam posted this in #help-forum
![Avatar](https://cdn.discordapp.com/avatars/546281136448339990/46ef93af3388358448dc4273d723f170.webp?size=256)
inamOP
Lets say I'm in a url
From here usinag a onclick event I want to send to
What is the best way to do it
I'm using Nextjs App router
localhost:3000/customer/[customerId]?folderId=252&subFolder=565
From here usinag a onclick event I want to send to
localhost:3000/customer/[customerId]?folderId=1255&subFolder=89898
What is the best way to do it
I'm using Nextjs App router
13 Replies
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
so do you want to change url, or send fetch request for the onclick?
if link, you can use next/navigation's useRouter.push or next/link if just a link button
![Avatar](https://cdn.discordapp.com/avatars/546281136448339990/46ef93af3388358448dc4273d723f170.webp?size=256)
inamOP
Yh I'm using a link
How to set the href properly ?
when I get the pathname from usePathname it only gives me
localhost:3000/customer/[customerId]
this![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
what about: [
useSearchParams
](https://nextjs.org/docs/app/api-reference/functions/use-search-params)but is this a client or server component
if server, cant you get the search parms from the page function too?
![Avatar](https://cdn.discordapp.com/avatars/546281136448339990/46ef93af3388358448dc4273d723f170.webp?size=256)
inamOP
so code would be
const pathname = usePathname()
const searchParam = useSearchParams()
const folderId = searchParam.get('folderId')
// and in Link
<Link href={`${pathname}?folderId=${folderId}&subFolder=${props.id}`} />
Its a client component
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
in threory this makes sense, does it work?
![Avatar](https://cdn.discordapp.com/avatars/546281136448339990/46ef93af3388358448dc4273d723f170.webp?size=256)
inamOP
Yh it works
I wanted to know is there a way just update that particular searchParam and route it to new url
I wanted to know is there a way just update that particular searchParam and route it to new url
![Avatar](https://cdn.discordapp.com/avatars/657067112434499595/aff4eeb34922ca980b0d5fbb5bc60c21.webp?size=256)
risky
look at this example, but basicly it is just using the same things and just making sure the others exist still https://nextjs.org/docs/app/api-reference/functions/use-search-params#updating-searchparams