Skip to content

How to update a query param using next js 13 app directory #47583

Answered by icyJoseph
jeanmayorga asked this question in Help
Discussion options

You must be logged in to vote

I think one way is to do this is to use the useRouter, usePathname, and useSearchParams hooks, with the URLSearchParams object API.

"use client";

import type { ChangeEvent } from "react";
import { usePathname, useRouter, useSearchParams } from "next/navigation";

const options = ["mew", "mewtwo", "pikachu"];

export const DropDown = ({ selected }: { selected: string }) => {
  const router = useRouter();
  const pathname = usePathname();
  const searchParams = useSearchParams();

  const onSelect = (event: ChangeEvent<HTMLSelectElement>) => {
    // now you got a read/write object
    const current = new URLSearchParams(Array.from(searchParams.entries())); // -> has to use this form

    /…

Replies: 23 comments 53 replies

Comment options

You must be logged in to vote
20 replies
@gjakubik
Comment options

@TakhirKudusov
Comment options

@cbovis
Comment options

@Bundas
Comment options

@franky47
Comment options

Answer selected by jeanmayorga
Comment options

You must be logged in to vote
18 replies
@lucasoz
Comment options

@lucasoz
Comment options

@bennettdams
Comment options

@Solexofficial
Comment options

@Chasmical
Comment options

Comment options

You must be logged in to vote
2 replies
@tar-aldev
Comment options

@abdoufma
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@V-iktor
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@PupoSDC
Comment options

Comment options

You must be logged in to vote
1 reply
@ilovett
Comment options

Comment options

You must be logged in to vote
1 reply
@Macumba45
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@gurupal
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@franky47
Comment options

@sugtao4423
Comment options

@ceIia
Comment options

@franky47
Comment options

Comment options

You must be logged in to vote
1 reply
@0xboga
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@Melancholism
Comment options

@SalahAdDin
Comment options

Comment options

You must be logged in to vote
1 reply
@kellyrmilligan
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet