skip to Main Content

I am creating platform that has pagination in layout. I don’t want to make this layout client side for SEO and Speed. How can I access my searchParams in layout component with SSR ?

2

Answers


  1. If you are using nextjs App router, you can use the userSearchParams hook to do that, simply by having the next code:

    'use client'
     
    import { useSearchParams } from 'next/navigation'
     
    export default function SearchBar() {
      const searchParams = useSearchParams()
     
      const search = searchParams.get('search')
     
      // URL -> `/dashboard?search=my-project`
      // `search` -> 'my-project'
      return <>Search: {search}</>
    }
    

    You can find further details in the official docs

    Login or Signup to reply.
  2. you should be able to do like below

    export default async function Page({ searchParams }: { searchParams: Promise<{ hello: string | undefined }> }) {
      const { hello } = await searchParams
      // your code here
    }
    

    Please check the docs https://nextjs.org/docs/app/api-reference/file-conventions/page#searchparams-optional

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search