Primer on Next.js App Router Nested Layouts

Ben Patton
InstructorBen Patton
Share this video with your friends

Social Share Links

Send Tweet
Published 9 months ago
Updated 8 months ago

Before we start migrating routes we will get a high level understanding of nested layouts in the app router

Instructor: [0:01] Nested layouts in the app router are really simple. As we've already seen, if we come to our app, we have our nav bar and we have our dashboard page. If we come to our dashboard page, we aren't having to recall our layouts.

[0:17] The layout in the root directory gets applied to the dashboard page as well. The big difference now is that in this dashboard route, we can create another layout.tsx file. Here, we can export the function.

[0:42] We want to pass it to children. We'll return a fragment. Then we'll return an h1 that says, The Dashboard Layout. If we save this first and we come back over here and refresh, we'll see that all we're getting is the dashboard layout.

[0:59] If we now call the children prop and save it, we are getting the dashboard layout and then the dashboard page. Whatever we add in the dashboard layout gets applied to the dashboard page. It would also apply to any subroutes under the dashboard route.

[1:17] In review, we saw how simple it is to create a nested layout in theAapp router. All that we needed to do was come into a subroute, create a layout.tsx file, pass the children as a prop, and then in the returned markup, just make sure that we put the children in the markup as well.

[1:36] Having done that, all of the markup will be applied to the subroute page and any further routes created in that subroute.

egghead
egghead
~ 7 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today