Duplicated render components
Answered
Sun bear posted this in #help-forum
Sun bearOP
Hello Everyone, my name is Rian and just learn NextJS here ðŸ™
I'm encountering a problem where the <header> and <main> tags are duplicating during navigation. In this case, I've set up my layout.jsx to enable the use of different header variants on each page. The duplication issue occurs as follows: when I first load the /berita page, the structure is rendered correctly as:
However, when I navigate to the home page and then return to the /berita page, the structure becomes incorrectly nested like this:
I'm encountering a problem where the <header> and <main> tags are duplicating during navigation. In this case, I've set up my layout.jsx to enable the use of different header variants on each page. The duplication issue occurs as follows: when I first load the /berita page, the structure is rendered correctly as:
<body>
<header>
</header>
<main>
</main>
</body>However, when I navigate to the home page and then return to the /berita page, the structure becomes incorrectly nested like this:
<body>
<header>
</header>
<main>
<header> <!-- This is the duplicated header -->
</header>
<main> <!-- This is the duplicated main -->
</main>
</main>
</body>99 Replies
If I'm not wrong if you have a root layout & another layout in route groups. the grouped route layout get nested inside the original layout.
@averydelusionalperson If I'm not wrong if you have a root layout & another layout in route groups. the grouped route layout get nested inside the original layout.
Sun bearOP
In my use case, I want to use a header variant on each specific page, that's why I'm not render the <header> and <main> on root layout.jsx instead I put it on layout.jsx on each route
is there anyway to avoid these duplicate issue?
you can remove root layout file and place layout file in multiple route groups
That's how I do it
I've something like this for my website, gonna add dashboard soon too, and it will have different layout
Note: I don't have root layout file in app dir
@averydelusionalperson I've something like this for my website, gonna add dashboard soon too, and it will have different layout
Sun bearOP
So, in that way we need to repeat an import things and return the html & body?
You can do something like this: https://stackoverflow.com/a/75971801
Do all imports and that stuff in root layout
but only have body & html in root layout
and add headers, footers, navbars, etc for each respective page/route group
@averydelusionalperson Do all imports and that stuff in root layout
Sun bearOP
Do you mean the root layout inside the (routes) folder? not app folder right?
app one?
I mean the app folder
either way, child is gonna inherit parent's layout If I'm not wrong, so don't add body & html in each one IG
Sun bearOP
this is my current structure
app/layout.jsx
app/layout.jsx
hmm, so you want footer in each page?
@averydelusionalperson hmm, so you want footer in each page?
Sun bearOP
yes, the footer will be same on all pages
okay sounds good
Sun bearOP
the different is the header
that's why, I'm using layout.jsx on each route and put the <Header variant>
but, the issue is my <header> and <main> is rendered twice during navigating
what about header, you want in some routes, and don't want in some?
@averydelusionalperson what about header, you want in some routes, and don't want in some?
Sun bearOP
the header will be on all routes, but with some variants
so you pass the variant prop?
Sun bearOP
this is my layout.jsx from /berita route
import Header from "../../../components/header";
const BeritaLayout = ({children}) => {
return (
<>
<Header />
<main>
{children}
</main>
</>
)
}
export default BeritaLayoutand this is layout.jsx from another route
import Header from "../../../../components/header";
const BeritaDetailLayout = ({children}) => {
return (
<>
<Header variant="seamless-light" />
<main>
{children}
</main>
</>
)
}
export default BeritaDetailLayout@averydelusionalperson so you pass the variant prop?
Sun bearOP
yes
both look good, so what's the problem? those routes aren't nested right?
@averydelusionalperson both look good, so what's the problem? those routes aren't nested right?
Sun bearOP
this is the problem
especially during navigating to another route
are those two routes on same level or nested?
@averydelusionalperson are those two routes on same level or nested?
Sun bearOP
a moment, I'll give you a sample video
Sun bearOP
yupss, enjoy your lunch
Ray to the rescue 👀
@Sun bear https://monosnap.com/file/PAEXDNgYmuqOTELdUjdWpQWcpFlbh6
Sun bearOP
1. First load, the components rendered correcly
2. goes to /berita route, components duplicated one time
3. goes to /berita/kolaborasi (dynamic route with slug param), components rendered twice
2. goes to /berita route, components duplicated one time
3. goes to /berita/kolaborasi (dynamic route with slug param), components rendered twice
@Sun bear this is my current structure
app/layout.jsx
I think you may want to create a
layout.tsx and render the Header and main tag there instead// app/(routes)/layout.tsx
export default function Layout({children}) {
return (
<>
<Header variant="seamless-light" />
<main>
{children}
</main>
</>
)
}@Ray I think you may want to create a `layout.tsx` and render the Header and main tag there instead
ts
// app/(routes)/layout.tsx
export default function Layout({children}) {
return (
<>
<Header variant="seamless-light" />
<main>
{children}
</main>
</>
)
}
Sun bearOP
Oh ok, so do I need to keep the layout.jsx on my root level?
@Sun bear Oh ok, so do I need to keep the layout.jsx on my root level?
its up to you, do you still need app/page.tsx there?
@Ray I think you may want to create a `layout.tsx` and render the Header and main tag there instead
ts
// app/(routes)/layout.tsx
export default function Layout({children}) {
return (
<>
<Header variant="seamless-light" />
<main>
{children}
</main>
</>
)
}
Sun bearOP
but, how i can pass the variant prop on each route?
eg. layout.jsx inside (routes)/berita/layout.jsx?
eg. layout.jsx inside (routes)/berita/layout.jsx?
@Ray its up to you, do you still need app/page.tsx there?
Sun bearOP
yes, it's just for the homepage
@Sun bear but, how i can pass the variant prop on each route?
eg. layout.jsx inside (routes)/berita/layout.jsx?
then I would render the <Header /> in the page instead
@Sun bear but, how i can pass the variant prop on each route?
eg. layout.jsx inside (routes)/berita/layout.jsx?
does berita contain subroute?
@Ray does berita contain subroute?
Sun bearOP
yes, dynamic route with param {slug}
@Ray then I would render the <Header /> in the page instead
Sun bearOP
I tried this one, but the header and main still rendered again inside the root main
are you importing the layout to the page?
@Ray are you importing the layout to the page?
Sun bearOP
Yes, at the first
have you tried to remove it?
next should wrap it for you
@Ray have you tried to remove it?
Sun bearOP
then I try your solution, which render the <Header /> inside the page.jsx, the problem still persist
@Sun bear then I try your solution, which render the <Header /> inside the page.jsx, the problem still persist
this is /berita/page.jsx right?
@Ray this is /berita/page.jsx right?
Sun bearOP
yupsss
let skip the dynamic route for /berita/{slug} first
@Sun bear yupsss
how does
app/layout.tsx and app/(routes)/layout.tsx look like?@Ray how does `app/layout.tsx` and `app/(routes)/layout.tsx` look like?
Sun bearOP
this is my /app/layout.jsx (no header and main there)
and I don't have layout.jsx on /app/(routes)
and I don't have layout.jsx on /app/(routes)
@Sun bear this is my /app/layout.jsx (no header and main there)
and I don't have layout.jsx on /app/(routes)
could you show the folder structure again?
with berita folder open
@Ray could you show the folder structure again?
Sun bearOP
yups, a moment
@Sun bear Click to see attachment
could you show the code on app/(routes)/berita/layout.tsx?
@Ray could you show the code on app/(routes)/berita/layout.tsx?
Sun bearOP
but, this one is not used again, since I render the <Header /> and <main> inside the page.jsx of /berita route
@Sun bear but, this one is not used again, since I render the <Header /> and <main> inside the page.jsx of /berita route
next will wrap the
page.jsx with layout.jsx automaticallyAnswer
@Sun bear then I try your solution, which render the <Header /> inside the page.jsx, the problem still persist
which mean you only need this in /berita/page.jsx
const Berita = () => {
return (
<>
<Hero />
</>
)
}
export default BeritaSun bearOP
this is my page.jsx on /app/(routes)/berita/page.jsx
@Ray which mean you only need this in /berita/page.jsx
ts
const Berita = () => {
return (
<>
<Hero />
</>
)
}
export default Berita
Sun bearOP
that is what I did on the first time, but the issue still persist 😕
@Sun bear that is what I did on the first time, but the issue still persist 😕
because you import the Layout to the page
try it again
@Ray because you import the Layout to the page
Sun bearOP
Oh you're right
a moment
@Ray because you import the Layout to the page
Sun bearOP
man, that is works 🙌
but, I just do the same thing on my dynamic route layout.jsx and the issue still persist
@Sun bear but, I just do the same thing on my dynamic route layout.jsx and the issue still persist
could you show that layout.tsx?
@Ray could you show that layout.tsx?
Sun bearOP
a moment
@Sun bear a moment
it shouldn't contain the
Header and main@Ray it shouldn't contain the `Header` and main
Sun bearOP
Yups, layout.jsx
try removing it
@Ray try removing it
Sun bearOP
But, i want to have another <Header /> variant there
as you can see I pass the variant="seamless-light" for "berita detail" route
@Sun bear as you can see I pass the variant="seamless-light" for "berita detail" route
either create a route-group or render the header on the page
app/(routes)/berita/(default)/layout.jsx <-- render <Header /> hereapp/(routes)/berita/(default)/berita/page.jsxapp/(routes)/berita/(seamless-light)/layout.jsx <-- render <Header variant="seamless-light" /> hereapp/(routes)/berita/(seamless-light)/berita/[slug]/page.jsx@Ray `app/(routes)/berita/(default)/layout.jsx` <-- render `<Header />` here
`app/(routes)/berita/(default)/berita/page.jsx`
`app/(routes)/berita/(seamless-light)/layout.jsx` <-- render `<Header variant="seamless-light" />` here
`app/(routes)/berita/(seamless-light)/berita/[slug]/page.jsx`
Sun bearOP
So, build the header variant based on route-group?
or check the
pathname in <Header /> since its a client component@Ray `app/(routes)/berita/(default)/layout.jsx` <-- render `<Header />` here
`app/(routes)/berita/(default)/berita/page.jsx`
`app/(routes)/berita/(seamless-light)/layout.jsx` <-- render `<Header variant="seamless-light" />` here
`app/(routes)/berita/(seamless-light)/berita/[slug]/page.jsx`
Sun bearOP
Got it, let me try this approach first,
Thank you so much for your time @Ray
I really appreciated that ðŸ™
I really appreciated that ðŸ™
Sun bearOP
I think I got the idea here @Ray
I just remove the layout.jsx from /berita/ & /berita/[slug]/
and render the <Header /> at the page.jsx on each route and the dynamic route,
I just remove the layout.jsx from /berita/ & /berita/[slug]/
and render the <Header /> at the page.jsx on each route and the dynamic route,
because the layout.jsx will be nested from the ancestor, it's better to put the <Header /> at the page.jsx, so the <Header /> rendered independently now
Again, big thanks @Ray
yeah that would work too
np