The App directory in Next.js helps organize your app's pages and components in a simple folder structure. It makes routing automatic and easy to manage.
App directory (App Router) in NextJS
Introduction
Syntax
NextJS
app/
page.js
layout.js
dashboard/
page.js
settings/
page.jsapp/ is the main folder for your app's routes.
Each folder inside app/ represents a route segment.
Examples
page.js file inside app/ is the homepage.NextJS
app/page.js
// This file creates the homepage at '/' routeapp/ with a page.js creates a nested route.NextJS
app/dashboard/page.js
// This file creates the '/dashboard' pagelayout.js file defines a shared layout for all pages inside app/.NextJS
app/layout.js
// This file wraps all pages with shared layout like header/footerSample Program
This example shows a root layout wrapping all pages with a header and footer. The homepage and dashboard pages show different content based on the route.
NextJS
/* app/layout.js */ export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header> <h1>My Next.js App</h1> </header> <main>{children}</main> <footer> <p>Ā© 2024 My App</p> </footer> </body> </html> ) } /* app/page.js */ export default function HomePage() { return <h2>Welcome to the homepage!</h2> } /* app/dashboard/page.js */ export default function DashboardPage() { return <h2>This is the dashboard.</h2> }
Important Notes
Each page.js file must export a React component as default.
Layouts wrap pages and can be nested for different route segments.
Use the app/ directory for new Next.js projects to get the latest routing features.
Summary
The app/ directory organizes routes by folders and files.
Each folder with a page.js file creates a route.
Layouts in layout.js wrap pages for shared UI.
Practice
1. In Next.js App Router, what does a folder containing a
page.js file represent?easy
Solution
Step 1: Understand the role of
In Next.js App Router, each folder with apage.jsin App Routerpage.jsfile defines a route that users can visit.Step 2: Differentiate from other files
Files likelayout.jsdefine layouts, not routes. API routes are inapi/folders, and static assets are inpublic/.Final Answer:
A route in the application -> Option AQuick Check:
Folder + page.js = route [OK]
Hint: Folder with page.js always means a route [OK]
Common Mistakes:
- Confusing layout.js with page.js
- Thinking page.js is for API routes
- Assuming static assets are routes
2. Which of the following is the correct way to define a layout in the Next.js App Router?
easy
Solution
Step 1: Identify layout file naming in App Router
Layouts are defined by creating alayout.jsfile inside the folder representing the route.Step 2: Exclude old or unrelated files
The_app.jsfile is used in the old pages router, not App Router.page.jsdefines pages, not layouts.server.jsis unrelated.Final Answer:
Create a layout.js file inside the route folder -> Option AQuick Check:
layout.js = layout file [OK]
Hint: Layouts use layout.js, not page.js or _app.js [OK]
Common Mistakes:
- Using _app.js instead of layout.js
- Confusing page.js with layout.js
- Trying to define layout in server.js
3. Given this folder structure in the
What happens when a user visits
app/ directory:app/
layout.js
page.js
dashboard/
layout.js
page.js
What happens when a user visits
/dashboard?medium
Solution
Step 1: Understand nested layouts in App Router
Layouts in parent folders wrap layouts and pages in child folders. So root layout wraps dashboard layout, which wraps dashboard page.Step 2: Confirm layout application order
Visiting /dashboard applies root layout first, then dashboard layout, then renders dashboard page inside.Final Answer:
The root layout wraps the dashboard layout, which wraps the dashboard page -> Option CQuick Check:
Layouts nest from root to child [OK]
Hint: Layouts nest from parent to child folders [OK]
Common Mistakes:
- Thinking only one layout applies
- Ignoring root layout when inside subfolder
- Assuming layouts do not nest
4. You wrote this code in
But when you visit
app/dashboard/page.js:export default function DashboardPage() {
return Dashboard
}
But when you visit
/dashboard, you get a blank page. What is the most likely cause?medium
Solution
Step 1: Verify root layout presence
In Next.js App Router, a rootlayout.jsfile in theapp/directory is required to render any pages, including nested ones.Step 2: Confirm naming and export correctness
Thepage.jsfile name is correct, and the component is exported as default. Class components are not required.Final Answer:
Missing layout.js in app/ folder -> Option DQuick Check:
Root layout required for all pages [OK]
Hint: Root layout.js required for nested pages [OK]
Common Mistakes:
- Renaming page.js to index.js incorrectly
- Forgetting default export
- Thinking class components are required
5. You want to create a nested route
/blog/[slug] using the App Router. Which folder and file structure is correct?hard
Solution
Step 1: Understand dynamic route syntax in App Router
Dynamic routes use square brackets around the parameter name as folder names, e.g.,[slug].Step 2: Confirm correct file placement
The dynamic folder[slug]contains apage.jsfile to render the dynamic route. Other options use incorrect naming or file placement.Final Answer:
app/blog/[slug]/page.js -> Option BQuick Check:
Dynamic routes use [param] folder with page.js [OK]
Hint: Dynamic routes use [param] folders with page.js inside [OK]
Common Mistakes:
- Using _slug instead of [slug]
- Placing dynamic param in filename instead of folder
- Omitting page.js inside dynamic folder
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
