I am unable to render layout.tsx
with a default theme as the theme provider is needed and useContext is called. Therefore next js complains about not being able to run server-side. So I put the use client
field into the document, and now the metadata tags won’t load.
layout.tsx
'use client'
import { Metadata } from 'next'
export const metadata: Metadata = {
title: {
default: 'some title',
template: '%s | some title'
}
}
import { createTheme, ThemeProvider } from '@mui/material/styles'
const theme = createTheme({...
…
export default ({children}: {children: React.ReactNode}) => (
<ThemeProvider theme={theme}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<html>
<body>{children}</body>
</html>
</LocalizationProvider>
</ThemeProvider>
)
2
Answers
Do not make your layout a client component, rather create a client component called Provider that then can be imported in your layout. Heres an example:
You need to split them in 2 files simple, one client & another server
Layout.tsx
RootLayout.tsx