The root layout is the main wrapper for all pages in a Next.js app. It helps keep common parts like headers or footers consistent everywhere.
Root layout (required) in NextJS
export default function RootLayout({ children }) { return ( <html lang="en"> <body> {/* Common UI like header */} {children} {/* Common UI like footer */} </body> </html> ) }
The children prop represents the page content inside the layout.
The root layout must be named layout.js or layout.tsx and placed in the app/ folder.
export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header>My Site Header</header> {children} <footer>My Site Footer</footer> </body> </html> ) }
<main> tag.export default function RootLayout({ children }) { return ( <html lang="en"> <body> <main>{children}</main> </body> </html> ) }
This root layout adds a styled header and footer around the page content. Every page will show the same header and footer automatically.
export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header style={{ padding: '1rem', backgroundColor: '#eee' }}> <h1>Welcome to My Next.js App</h1> </header> <main style={{ padding: '1rem' }}> {children} </main> <footer style={{ padding: '1rem', backgroundColor: '#eee', marginTop: '2rem' }}> <p>Ā© 2024 My Next.js App</p> </footer> </body> </html> ) }
The root layout wraps all pages, so changes here affect the whole app.
Use semantic HTML tags like <header>, <main>, and <footer> for better accessibility.
Remember to set the lang attribute on the <html> tag for screen readers and SEO.
The root layout is the main wrapper for all pages in Next.js.
It helps keep common UI and settings consistent across the app.
Always place it in the app/ folder as layout.js or layout.tsx.
Practice
Solution
Step 1: Understand the role of root layout
The root layout wraps all pages and components, providing a shared structure.Step 2: Identify what it manages
It keeps common UI elements and settings consistent across the app.Final Answer:
To wrap all pages and keep common UI consistent -> Option CQuick Check:
Root layout = common UI wrapper [OK]
- Confusing root layout with API routes
- Thinking root layout manages database
- Assuming root layout handles only client state
Solution
Step 1: Recall Next.js 14+ root layout location
The root layout must be namedlayout.jsorlayout.tsxinside theapp/folder.Step 2: Check options for correct file and folder
Onlyapp/layout.jsmatches the required name and location.Final Answer:
app/layout.js -> Option AQuick Check:
Root layout = app/layout.js [OK]
- Using pages/_app.js instead of app/layout.js
- Naming file rootLayout.jsx instead of layout.js
- Placing layout in components folder
app/layout.js, what will be rendered on every page?export default function RootLayout({ children }) {
return (
My Site Header
{children}
)
}Solution
Step 1: Analyze the returned JSX structure
The root layout returns anhtmlelement withbodycontaining header, children, and footer.Step 2: Understand children prop usage
The{children}placeholder renders the page content inside the layout.Final Answer:
Header, children content, and footer on every page -> Option DQuick Check:
Root layout renders header + children + footer [OK]
- Thinking children is ignored
- Believing <html> tag causes error here
- Assuming only children render without header/footer
app/layout.js? export default function RootLayout({ children }) {
return (
Site Header
{children}
)
}Solution
Step 1: Recall Next.js root layout requirements
The root layout must return anhtmlelement wrapping the entire page content.Step 2: Identify the missing
This code returns ahtmltagdivinstead ofhtml, which breaks Next.js layout rules.Final Answer:
Root layout must return an <html> element wrapping content -> Option BQuick Check:
Root layout requires <html> wrapper [OK]
- Using div instead of html as root element
- Forgetting to include children prop
- Misnaming the layout function
app/layout.js?export default function RootLayout({ children }) {
return (
My App
{children}
)
}Solution
Step 1: Understand Next.js 14+ root layout capabilities
The root layout can include<head>to add metadata and global styles.Step 2: Verify usage of
Including a<head>for fonts and titlelinkfor fonts and atitleinside<head>is the correct way to add global styles and metadata.Final Answer:
This is correct; use <head> inside root layout for metadata and fonts -> Option AQuick Check:
Use <head> in root layout for global metadata [OK]
- Thinking <head> is disallowed in root layout
- Importing fonts only in individual pages
- Setting metadata only in config files
Ready to prove it?
Pass the test and this pattern moves unknown
