#search params on router replace on next 13

1 messages · Page 1 of 1 (latest)

ember onyx
#

how to pass search params on router replace on next 13

red adderBOT
#

Hi @ember onyx. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

In Next.js 13, you can use the useRouter hook from next/navigation to replace the current route and pass search parameters. Here's an example of how you can do this:

import { useRouter, usePathname, useSearchParams } from 'next/navigation'
import { useCallback } from 'react'

export default function ExampleClientComponent() {
 const router = useRouter()
 const pathname = usePathname()
 const searchParams = useSearchParams()

 // Get a new searchParams string by merging the current
 // searchParams with a provided key/value pair
 const createQueryString = useCallback(
 (name: string, value: string) => {
 const params = new URLSearchParams(searchParams)
 params.set(name, value)
 
 return params.toString()
 },
 [searchParams]
 )

 return (
 <button
 onClick={() => {
 // <pathname>?sort=asc
 router.replace(pathname + '?' + createQueryString('sort', 'asc'))
 }}
 >
 Sort Ascending
 </button>
 )
}

In this example, we're using the useRouter hook to get access to the router object, and the usePathname and useSearchParams hooks to get the current pathname and search parameters. We then create a new query string with the desired search parameters using the URLSearchParams API, and use router.replace to navigate to the new URL.

Please note that the useRouter, usePathname, and useSearchParams hooks should be imported from next/navigation and not next/router in Next.js 13. Also, these hooks are only supported in Client Components and cannot be used in Server Components.

#

@ember onyx kapa.ai is still learning and improving, please let us know how it did by reacting below

ember onyx
#

can i use it on next 13 with app folder?

#

@red adder can i use it on next 13 with app folder?

red adderBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, you can use the useRouter, usePathname, and useSearchParams hooks in the app directory in Next.js 13. These hooks are part of the new routing system introduced in Next.js 13 and are specifically designed to be used in the app directory.

Here's an example of how you can use these hooks in a component in the app directory:

'use client'
 
import { useRouter, usePathname, useSearchParams } from 'next/navigation'
 
export default function ExampleClientComponent() {
 const router = useRouter()
 const pathname = usePathname()
 const searchParams = useSearchParams()
 
 // ...
}

Please note that the useRouter hook imported from next/router is not supported in the app directory but can continue to be used in the pages directory. The new useRouter does not return the pathname string or the query object. Instead, you should use the separate usePathname and useSearchParams hooks.

For more information, you can refer to the Next.js documentation (https://nextjs.org/docs/app/api-reference/functions/use-router) and the App Router Migration Guide (https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@ember onyx kapa.ai is still learning and improving, please let us know how it did by reacting below

ember onyx
#

@red adder no! i need to use the code bellow in next 13

router.replace(
{
pathname: pathname,
search: ?${queryParams.toString()},
},
undefined,
{ shallow: true },
);

red adderBOT
#

Let me check the knowledge base for you. One moment! :books:

#

In Next.js 13, the router.replace method from next/router has been replaced with a simpler API that only accepts a URL string as its first argument. The new router.replace method from next/navigation does not support passing an object with pathname and search properties or options like { shallow: true }.

Here's how you can rewrite your code to work with Next.js 13:

import { useRouter, usePathname, useSearchParams } from 'next/navigation'

export default function ExampleClientComponent() {
  const router = useRouter()
  const pathname = usePathname()
  const searchParams = useSearchParams()

  // ...

  const queryParams = new URLSearchParams(searchParams)
  // Set your query parameters here
  // queryParams.set('key', 'value')

  router.replace(`${pathname}?${queryParams.toString()}`)
}

In this example, we're using the useRouter, usePathname, and useSearchParams hooks to get the current router object, pathname, and search parameters. We then create a new query string with the desired search parameters using the URLSearchParams API, and use router.replace to navigate to the new URL.

Please note that the useRouter, usePathname, and useSearchParams hooks should be imported from next/navigation and not next/router in Next.js 13. Also, these hooks are only supported in Client Components and cannot be used in Server Components.

For more information, you can refer to the Next.js documentation (https://nextjs.org/docs/app/api-reference/functions/use-router) and the App Router Migration Guide (https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: