Client-Component Wraps Server-Component
Answered
sudo posted this in #help-forum
sudoOP
Hello,
NextJS 14 App Router.
If in the layout.tsx file,
I have this layout.
If
This means that all pages inherting this layout, and all the components in these pages, will be client-components?
Will i lose the NextJS Server-side rendering aspect for these pages/components?
NextJS 14 App Router.
If in the layout.tsx file,
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>
<NotiProvider>
<Navbar></Navbar>
{children}
</NotiProvider>
</body>
</html>
)
}I have this layout.
If
NotiProvider is defined in src/app/context/NotiContext.tsx which is marked with use client. This means that all pages inherting this layout, and all the components in these pages, will be client-components?
Will i lose the NextJS Server-side rendering aspect for these pages/components?
9 Replies
@sudo Hello,
NextJS 14 App Router.
If in the layout.tsx file,
`export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>
<NotiProvider>
<Navbar></Navbar>
{children}
</NotiProvider>
</body>
</html>
)
}`
I have this layout.
If `NotiProvider` is defined in `src/app/context/NotiContext.tsx` which is marked with `use client`.
This means that all pages inherting this layout, and all the components in these pages, will be client-components?
Will i lose the NextJS Server-side rendering aspect for these pages/components?
no, you won't. If you pass server components in like this as children, they are still server components
@B33fb0n3 no, you won't. If you pass server components in like this as children, they are still server components
sudoOP
Even if they are wrapped in a client-component?
If you have
yes
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body className={inter.className}>
<Provider> // clientside
{children} // server component
</Provider>
</body>
</html>
)
}yes
Answer
sudoOP
Much appreciated! Thanks @B33fb0n3
sudoOP
on it
sudoOP
Also, found this doc for this specific use case: https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#using-context-providers