Next.js Discord

Discord Forum

How to retrieve Search Params in a route?

Answered
Great black wasp posted this in #help-forum
Open in Discord
Great black waspOP
I have client component buttons adding search parameters and my URL ends up looking something like this:
http://localhost:3000/verses?query=test&section=old
However, in verses/page.tsx, when I try to destructure the search parameters and output them on the console, it always shows undefined... can someone explain why?

import SearchBar from "@/components/verses/SearchBar";
import SideBar from "@/components/verses/SideBar";

const Database = ({ section, query } : { section : string, query: string }) => {

  console.log(section)

  return (
    <div className="mt-10 max-w-7xl px-6 mx-auto">
      <SearchBar />
      <SideBar />
    </div>
  );
};

export default Database;


Sorry if this is a stupid question, I just started react and nextjs a few weeks back
Answered by @ts-ignore
const Database = ({searchParams})
View full answer

9 Replies

@@ts-ignore the pages get a `searchParam` which is an object having all search params
Great black waspOP
okay let me test it out
@@ts-ignore the pages get a `searchParam` which is an object having all search params
Great black waspOP
for some reason it still comes out as undefined
my bad it is searchParams(notice the s at the end)
Great black waspOP
this is what you meant right
const Database = (SearchParams) => {

  console.log(SearchParams.section)
no
Great black waspOP
oh
const Database = ({searchParams})
Answer
@@ts-ignore ts const Database = ({searchParams})
Great black waspOP
tysmmm :)