📖 Scenario: You are building a simple website with Next.js. You want to understand how to use layouts and templates to organize your pages. Layouts help you keep common parts like headers and footers consistent. Templates help you create page structures that can change content but keep the same style.
🎯 Goal: Build a Next.js app with a RootLayout that wraps all pages and a DashboardTemplate that structures dashboard pages. See how layouts and templates differ in usage and structure.
📋 What You'll Learn
Create a RootLayout component with a header and footer
Create a DashboardTemplate component with a sidebar and main content area
Use the RootLayout to wrap the entire app
Use the DashboardTemplate inside a dashboard page
💡 Why This Matters
🌍 Real World
Web developers use layouts and templates in Next.js to build websites with consistent structure and reusable page designs.
💼 Career
Understanding layouts and templates is essential for building scalable Next.js applications and working in teams on professional web projects.
Progress0 / 4 steps
1
Create the RootLayout component
Create a file app/layout.tsx with a React functional component called RootLayout. It should return an HTML structure with <html>, <body>, a <header> containing the text "Site Header", and a <footer> containing the text "Site Footer". The component should accept a children prop and render it inside the <body> between header and footer.
NextJS
Hint
Remember to wrap children between header and footer inside the body tag.
2
Add a configuration variable for sidebar items
Create a file app/dashboard/template.tsx. Inside it, create a constant array called sidebarItems with these exact strings: 'Home', 'Profile', 'Settings'. This will be used to show sidebar links in the dashboard template.
NextJS
Hint
Use a constant array with the exact names for sidebar items.
3
Create the DashboardTemplate component using sidebarItems
In app/dashboard/template.tsx, create a React functional component called DashboardTemplate that accepts a children prop. It should render a <div> with two sections: a <nav> listing each item from sidebarItems inside <li> elements within a <ul>, and a <main> that renders the children. Use .map() to create the list items. Make sure to add a key prop to each <li>.
NextJS
Hint
Use sidebarItems.map(item => <li key={item}>{item}</li>) to list sidebar items.
4
Use DashboardTemplate inside a dashboard page
Create a file app/dashboard/page.tsx with a React functional component called DashboardPage that is the default export. Inside it, return the DashboardTemplate component wrapping a <p> with the text "Welcome to the dashboard". Import DashboardTemplate from ./template. This shows how templates are used inside pages.
NextJS
Hint
Import DashboardTemplate and use it to wrap the paragraph inside DashboardPage.
Practice
(1/5)
1. In Next.js, what is the main role of a layout component?
easy
A. To manage client-side state
B. To provide common page structure like headers and footers
C. To handle API requests
D. To define unique content for each page
Solution
Step 1: Understand layout purpose
Layouts in Next.js wrap pages to provide shared UI elements like headers and footers.
Step 2: Differentiate from templates
Templates hold unique page content, not common structure.
Final Answer:
To provide common page structure like headers and footers -> Option B
Quick Check:
Layout = common structure [OK]
Hint: Layouts wrap pages with shared UI elements [OK]
Common Mistakes:
Confusing layouts with templates
Thinking layouts handle data fetching
Assuming layouts manage state
2. Which of the following is the correct way to define a layout in Next.js 14+ using the App Router?
easy
A. export default function Layout({ children }) { return
{children}
}
B. function Layout() { return
Page Content
}
C. const Layout = () =>
Header
D. export function layout() { return {children} }
Solution
Step 1: Check export and props
Layouts must be default exported and receive children prop to wrap page content.
Step 2: Validate syntax
export default function Layout({ children }) { return
{children} } correctly exports a function with { children } and returns JSX wrapping children.
Final Answer:
export default function Layout({ children }) { return <div>{children}</div> } -> Option A
Quick Check:
Default export + children prop = export default function Layout({ children }) { return
{children}
} [OK]
Hint: Layouts must export default and accept children [OK]
Common Mistakes:
Not exporting default
Missing children prop
Using lowercase function name for layout
3. Given this Next.js layout component:
export default function Layout({ children }) {
return (
<html>
<body>
<header>Site Header</header>
{children}
<footer>Site Footer</footer>
</body>
</html>
)
}
And a page component:
export default function Page() {
return <main>Welcome!</main>
}
What will be the rendered HTML structure?
medium
A. Site HeaderWelcome!
B. Welcome!
C. Site Header
D. Welcome!
Solution
Step 1: Understand layout wrapping
The layout wraps the page content inside <html> and <body> tags, adding header and footer around children.
Step 2: Insert page content
The page returns <main>Welcome!</main>, which is placed where {children} is in layout.
Final Answer:
<html><body><header>Site Header</header><main>Welcome!</main><footer>Site Footer</footer></body></html> -> Option A
Quick Check:
Layout wraps page content with header/footer [OK]
Hint: Children content appears inside layout's wrapper [OK]
But the page content does not render inside the layout. What is the problem?
medium
A. The footer tag is invalid in layouts
B. The layout should not have a header
C. The layout function does not accept the children prop
D. The layout must be a class component
Solution
Step 1: Check function parameters
The layout must accept { children } as a parameter to render page content inside.
Step 2: Identify missing children prop
The code misses { children } in the function signature, so it cannot render page content.
Final Answer:
The layout function does not accept the children prop -> Option C
Quick Check:
Missing children prop causes no page content [OK]
Hint: Layouts must accept children prop to render page content [OK]
Common Mistakes:
Forgetting to add children parameter
Thinking header/footer cause error
Believing layouts must be class components
5. You want to create a Next.js page that uses a layout with a sidebar and header. The sidebar should only appear on some pages, but the header should always be present. How should you organize layouts and templates to achieve this?
hard
A. Add sidebar inside each page component instead of layouts
B. Put header and sidebar in the same layout used by all pages
C. Use templates only and avoid layouts for shared UI
D. Create a main layout with header, then nested layouts for pages with sidebar
Solution
Step 1: Identify shared and conditional UI
The header is shared by all pages, so it belongs in the main layout. Sidebar is conditional, so it fits in nested layouts.
Step 2: Use nested layouts
Create a main layout with header, then nest layouts for pages needing sidebar to add it without affecting others.
Final Answer:
Create a main layout with header, then nested layouts for pages with sidebar -> Option D
Quick Check:
Nested layouts handle conditional UI [OK]
Hint: Use nested layouts for conditional UI parts [OK]
Common Mistakes:
Putting all UI in one layout causing unwanted sidebar
Adding sidebar directly in pages losing layout benefits
Avoiding layouts and mixing shared UI in templates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.