Next.js Discord

Discord Forum

Set searchParams

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Asiatic LionOP
Is there a way to SET searchParams?

I know how the get works ex:
const searchParams = useSearchParams();
const pagina = searchParams.get("pagina");
I would like to set a value but i cannot find a way.
Answered by averydelusionalperson
in your case: router.push(`pagina=${paginiaNewValue}`)
View full answer

6 Replies

American Crow
You basically just add or remove them from the URL, e.g.
const searchParams = useSearchParams()
const selectedRegeion = searchParams.get("region") ?? ""
const {replace} = useRouter()
const pathname = usePathname()

 function removeRegionParam() {
    const params = new URLSearchParams(searchParams)
    params.delete("region")
    replace(`${pathname}?${params.toString()}`)
}

or in a basic way using Link <Link href="?region=eu">EU</Link>
I just do router.push("?sup=hello)
in your case: router.push(`pagina=${paginiaNewValue}`)
Answer
Asiatic LionOP
Thank guys!
I ended up using this way
in your case: router.push(pagina=${paginiaNewValue}) with a little tweak