I am working on a Next.js 13
project where I have a route named problems/[slug]
. On the first level of this route (e.g., problems/react)
, I have a sidebar and a content section, which I am displaying using a layout.tsx
. However, on the next nested level of the route (e.g., /problems/react/test-problem)
, I want to use a different layout instead of the original one. Can you suggest a way to achieve this?
Note : I am looking for app directory based approach
3
Answers
Referring to my comment on the post, there should be no issue in what you’re trying to do.
You can create a component called
<ParentLayout />
and use it as the layout forproblems/react
, and use a different component<ChildLayout />
forproblems/react/test-problem
.Import the appropriate components into the respective files, and use them.
You can keep the structure of the pages folder as shown below
If you need multiple layouts, you can add a property
getLayout
to your page, allowing you to return a React component for the layout. This allows you to define the layout on a per-page basis. Since we’re returning a function, we can have complex nested layouts if desired.Update your pages/_app.js file
Learn more about it on Next.js documentation here
in Next 13 you can use the usePathname hook to determine which page you are on and then logically show or hide it: https://beta.nextjs.org/docs/api-reference/use-pathname
const pathname = usePathname(); const show_Navbar_Sidebar = pathname === '/about' || pathname === '/signup' ? false : true;