Next.js Discord

Discord Forum

[Newbie] Next.JS Layout Issue

Answered
Common carp posted this in #help-forum
Open in Discord
Avatar
Common carpOP
Hello,

I previously used Next.js 13.8 where I had the liberty to structure my components like this without any hitches:
export default function RootLayout() {
  return (
    <>
      <NavBar />
      <Section />
    </>
  );
}

However, with the updated layout in the new version, it seems the format has changed to:
export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <>
      <html lang="en">
        <body>{children}</body>
      </html>
    </>
  );
}

When I try to place <NavBar /> above the <body> element, it still renders below it. And if I omit the html tag, reverting to my previous format:
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <>
      <NavBar />
      <body>{children}</body>
    </>
  );
}

The elements don't render at all, and the page doesn't refresh as expected. The documentation doesn’t seem to highlight the significance of the html tag. Could someone clarify if there's a new requirement or if I'm missing something here? Thank you!
Answered by Common carp
So if I want to add Section to a particular page, I'd do it through page.tsx?
View full answer

41 Replies

Avatar
drewb
try putting it in the body tag
Avatar
Common carpOP
Hmmm yes, now I see it.
So what's the page.tsx for?
Avatar
West African Lion
<html lang="en"> <body><Navbar />{children}</body> </html>
Avatar
Common carpOP
Yes it worked
Avatar
drewb
that's the page itself, basically {children}
Avatar
Common carpOP
Right right, but now in the sense that page.tsx is the body aspect of what was there before, should I just export page.tsx as a Home component?
Avatar
drewb
every page you create will have the layout applied, where the page html is where children is
Avatar
Common carpOP
And I can remove body and children altogether?
Avatar
drewb
you'll want to keep both in the layout
Avatar
Common carpOP
This is my current structure. The way I'm used is to separate each "section" of my website into components
Image
Kinda like:
export default function RootLayout() {
  return (
    <>
      <NavBar />
      <Section />
<Section />
<Section />
<Section />
<Footer />
    </>
  );
}
Avatar
drewb
RootLayout is basically stuff that will get applied to every page you create
Avatar
Common carpOP
Thanks for helping out btw, you've really cleared up lots of my confusion
Avatar
drewb
so you generally want your Navbar, Footer, and that kind of stuff there
the "page.tsx" files you create are going to be the dynamic stuff
Avatar
Common carpOP
Ohhh I see. So that's for the layout of each page (like you stated).
Avatar
Common carpOP
So if I want to add Section to a particular page, I'd do it through page.tsx?
Answer
Avatar
drewb
Yep! So it's just like your constants across all pages.
Avatar
Common carpOP
Gotcha gotcha
Avatar
drewb
And yes, sections should usually go in page.tsx
There's always exceptions 😂
Avatar
Common carpOP
Cool! Many thanks
Avatar
drewb
If my biology coursework has taught me anything...
Avatar
Common carpOP
Yea haha I'm not doing anything crazy just a simple one page ecommerce (like for a 1 product mead bottle lol)
Avatar
drewb
So yes, I think you could in theory put sections in the layout. If you wanted to create additional pages later on though you'd have to migrate the code anyway. Also just best practice imo
Avatar
Common carpOP
According to the docs, additional pages means just creating a folder called pages, and adding stuff there right?
OH
It's associted to the file tsx
Which then becomes a slug
Avatar
drewb
If I wanted to create a page at /about, I'd
1. Create folder at app/about
2. Create page.tsx in that folder
Avatar
Common carpOP
I see I see
Damn thanks haha I learned a lot
I'm used to vite so this is a real kicker. Thanks
Avatar
drewb
that feature got me hooked on next haha
i love it
Avatar
Common carpOP
Yea, it already sounds like I'll love it
Avatar
drewb
i went from create-react-app and react-router to that and my mind was blown
Avatar
Common carpOP
Well said. So much more intuitive I think
Anyway, thanks for the help. I'll pass along this knowledge to others.
Avatar
drewb
Ofc!