Nested layouts and routes in Next.js

Jonas Grøndahl Petersen · 24K views · 2020 · 10:56

Open on YouTube

Web developmenttutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering how to wrap only some Next.js pages in a shared layout or context provider, or how to get react-router style nested routes.

Recommended by 1 people across 1 different months, 2021 to 2023.

What it is

A short screencast by Jonas Grøndahl Petersen showing how he built nested layouts and tab-based routes in a Next.js app. It covers reusing shared markup across subpages with the Next.js router, an approach that is trickier than in react-router, and credits an article from reacttricks.com.

What people said

Recommends Code Copier · 2021-08 · Stack Overflow · on “How do I only wrap some pages in a Context Provider with Next.js?”

There is workaround to wrap components of a specific path with a wrapper without react-router. Check out this video: [this video] The video shows you how to wrap components of a sub-path with a nested layout. Create a nested layout and wrap that that layout with your Context Provider.

Cites Ben Bagley · 2021-12 · Stack Overflow · on “Render nextjs page using layout - has already been declared error”

No luck @Hughes, checking the docs it seems there importing but others don't import I've looked at this video too [this video]

Cites user12457151 · 2022-04 · Stack Overflow · on “NextJS - Nested routing + subpages with context and layout”

I'm currently in the process of migrating one of my react apps to NextJS and am struggling to fit my previous react-router architecture into the nextjs routing. The page I'm building would look something like this: [LINK] When a user goes to a collection/:collectionId, the banner image along with some basic details are fetched. This data should persist across the 3 subpages and should not be refetched, so I want a CollectionContext that wraps the three tabs. Then for each tab, there are 3 separate data fetches depending on which tab the user is in: collection/:collectionid/items collection/:collectionid/analytics collection/:collectionid/auctions I would like to have 3 separate contexts for

Cites Sanu Kumar · 2023-07 · Stack Overflow · on “Is there a way to achieve outlets in next.js ? in regular react router i could use <outlet> to render child components”

you can use something like this, app/layout.js // - Applies to all routes export default function RootLayout({ children }) { return ( <html> <body> <Header /> {children} <Footer /> </body> </html> ); } app/dashboard/layout.js // - Applies to route segments in app/dashboard/* export default function DashboardLayout({ children }) { return ( <> <DashboardSidebar /> {children} </> ); } If you want to know more, refer this: [LINK] [this video]