Next.js Discord

Discord Forum

How to retrieve query params in a layout.tsx file

Answered
British Shorthair posted this in #help-forum
Open in Discord
Avatar
British ShorthairOP
First of all, here's my current app dir structure

app
|__ (jobs)
   |__ layout.tsx
   |__ jobs/
      |__ layout.tsx
      |__ page.tsx
      |__ [id]/
          |__ page.tsx


The jobs group has a layout that has a top bar that contains a filter input and inside the jobs folder, I have another layout where I am fetching all the jobs that need to be filtered based on the criteria that were set through the prior layout.

In the app/(jobs)/layout.tsx file, I have a layout that contains a filter input that mutates the query parameters in the /jobs route path. Such as /jobs?category=full_time&salary=50+ or /jobs/[jobId]?category=full_time&salary=50+

To filter out jobs, I must access those query params in the app/(jobs)/jobs/layout.tsx file.

I tried adding an api call to a route handler to the layout file but got this error:

Error: connect ECONNREFUSED ::1:80
jobseeker:dev:     at RedirectableRequest.emit (node:events:514:28)
jobseeker:dev:     at ClientRequest.emit (node:events:514:28)
jobseeker:dev:     at Socket.socketErrorListener (node:_http_client:501:9)
jobseeker:dev:     at Socket.emit (node:events:514:28)
jobseeker:dev: digest: "505303072"


Is this the way to go to implement this feature? Please help me out with clarity and guidance.

Please feel free to ask questions for more context. Thanks!
Image
Answered by Ray
you could use parallel route for the sidebar
View full answer

23 Replies

Avatar
Ray
layout.tsx does not receive searchParams. Could you fetch the data in page.tsx instead?
Avatar
British ShorthairOP
I need it in the layout.tsx file
Because the sidebar will show all the jobs that will stick beside (/jobs/layout.tsx) the job details page (/jobs/[id])
Avatar
Ray
you could use parallel route for the sidebar
Answer
Avatar
British ShorthairOP
How would the folder structure look like in that case?
Avatar
Ray
where is your sidebar render?
Avatar
British ShorthairOP
Inside the (jobs)/jobs/layout.tsx
Avatar
Ray
create it in (jobs)/jobs/@sidebar/default.tsx
it will receive searchParams
Avatar
British ShorthairOP
Interesting
Trying it
Avatar
Ray
// (jobs)/jobs/layout.tsx
export default function Layout({children, sidebar}) {
  return (
    {sidebar}
    {children}
  )
}

then render it in the layout.tsx
Avatar
British ShorthairOP
// (jobs)\jobs\@sidebar\default.tsx
import JobsList from "@/components/jobs/List.server";

export default function JobsListSideBar() {
  return (
    <div>
      <JobsList />
    </div>
  );
}


// (jobs)\jobs\layout.tsx
export default async function JobsRootPageLayout(
  props: AppProps & { sidebar: ReactNode },
) {
  const { children, sidebar } = props;

  console.log({ sidebar, children });
  return (
    <div className="flex h-full w-full items-center justify-center">
      <aside className="flex h-full w-full max-w-[350px] flex-col border-r bg-white">
        <div className="h-full w-full overflow-x-hidden">{sidebar}</div>
        <div className="h-full w-full flex-1 overflow-x-hidden p-4">
          {children}
        </div>
      </div>
  );
}


Getting undefined while logging out the sidebar
Image
Avatar
Ray
restart the dev server
Avatar
British ShorthairOP
Still the same
Avatar
Ray
remove .next and restart again
Avatar
British ShorthairOP
💥
Worked
I can also access the searchParams now!
You're a genius @Ray !
Thank you so much for the help
I appreciate
Avatar
Ray
you're welcome