⊗jsnxPmLtNL 42 of 57 menu

Nested Layouts in NextJS

As you know, the main site layout is located in the file /app/layout.jsx within the /app folder.

However, if desired, you can create your own layouts in nested folders. In this case, the page content will first be inserted into the nested layout, and then the resulting result will be inserted into the main layout.

Let's try it in practice. Let our main layout have the following form:

export default function RootLayout({children}) { return ( <html lang="en"> <body> <header> header </header> <main> {children} </main> <footer> footer </footer> </body> </html> ); }

Let's create another, nested layout for all content from the /users folder:

export default function UsersLayout({children}) { return ( <> <div className="info"> message for all users </div> <div className="content"> {children} </div> </> ); }

As you can see, in this layout we do not specify the main page tags, as they are already present in our main site layout.

Let's also create a content file to display the user:

export default function User() { return <> <h1>User</h1> <p> user description </p> </>; }

After these manipulations, our file structure will look like this:

  • /app/
    • layout.jsx
    • /users/
      • layout.jsx
      • /show/
        • page.jsx

Now, if you go to the corresponding URL, NextJS will put everything together and in the browser we will get the following final HTML code:

<html lang="en"> <body> <header> header </header> <main> <div class="info"> message for all users </div> <h1>User</h1> <p> user description </p> </main> <footer> footer </footer> </body> </html>

Create a users section on the site and a posts section. In each section, create your own layout that will inherit from the main site layout.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline