📖 Scenario: You are building a simple website with multiple pages organized in folders to create nested routes.This helps visitors navigate easily and keeps your code neat.
🎯 Goal: Create nested routes in a Next.js app using folders inside the app directory.Build a homepage, an about page inside an about folder, and a contact page inside a contact folder.
📋 What You'll Learn
Create a homepage at app/page.tsx with a heading 'Home Page'.
Create an about folder inside app with a page.tsx file showing heading 'About Us'.
Create a contact folder inside app with a page.tsx file showing heading 'Contact Us'.
Use React functional components with default exports for each page.
💡 Why This Matters
🌍 Real World
Websites often have multiple pages grouped by topic or function. Using nested folders for routes keeps code organized and URLs user-friendly.
💼 Career
Understanding Next.js routing and folder structure is essential for building scalable React applications and working in modern web development teams.
Progress0 / 4 steps
1
Create the homepage component
Create a file app/page.tsx with a React functional component named HomePage that returns an <h1> with the text 'Home Page'. Export it as default.
NextJS
Hint
Use export default function HomePage() { return <h1>Home Page</h1>; }
2
Add the About page in a folder
Inside the app folder, create a new folder named about. Inside about, create a file page.tsx with a React functional component named AboutPage that returns an <h1> with the text 'About Us'. Export it as default.
NextJS
Hint
Create app/about/page.tsx with export default function AboutPage() { return <h1>About Us</h1>; }
3
Add the Contact page in a folder
Inside the app folder, create a new folder named contact. Inside contact, create a file page.tsx with a React functional component named ContactPage that returns an <h1> with the text 'Contact Us'. Export it as default.
NextJS
Hint
Create app/contact/page.tsx with export default function ContactPage() { return <h1>Contact Us</h1>; }
4
Verify folder structure and exports
Ensure your app folder has page.tsx for the homepage, and inside app/about and app/contact folders, each has a page.tsx exporting the correct component. Confirm all components are default exports.
NextJS
Hint
Check that all three components are default exported and located in correct folders.
Practice
(1/5)
1. In Next.js 14 with the App Router, what does placing a page.tsx file inside a nested folder under app/ do?
easy
A. Bundles all pages into a single route regardless of folder structure.
B. Overrides the root page.tsx and disables routing.
C. Creates a nested route matching the folder path in the URL.
D. Automatically generates API endpoints instead of pages.
Solution
Step 1: Understand folder structure in Next.js App Router
Each folder inside app/ represents a route segment in the URL.
Step 2: Role of page.tsx in nested folders
A page.tsx inside a nested folder creates a page for that nested route path.
Final Answer:
Creates a nested route matching the folder path in the URL. -> Option C
5. You want to create a nested route /profile/settings/privacy in Next.js App Router. You also want to share a layout between /profile/settings and /profile/settings/privacy. How should you organize your folders and files inside app/?
hard
A. app/profile/settings/privacy/page.tsx only; layouts are not needed for nested routes
B. app/profile/settings/layout.tsx, app/profile/settings/page.tsx, app/profile/settings/privacy/page.tsx
C. app/profile/layout.tsx, app/profile/settings/page.tsx, app/profile/settings/privacy/page.tsx
D. app/profile/settings/page.tsx, app/profile/settings/privacy/layout.tsx, app/profile/settings/privacy/page.tsx
Solution
Step 1: Understand layout sharing in nested routes
Layouts placed in a folder apply to all nested routes inside that folder.
Step 2: Organize folders for shared layout
To share layout between /profile/settings and /profile/settings/privacy, place layout.tsx inside settings/ folder.
Step 3: Place page.tsx files correctly
Put page.tsx inside settings/ and inside settings/privacy/ for those pages.
Final Answer:
app/profile/settings/layout.tsx, app/profile/settings/page.tsx, app/profile/settings/privacy/page.tsx -> Option B
Quick Check:
Layout in parent folder shares UI with nested pages [OK]
Hint: Put layout.tsx in parent folder to share with nested routes [OK]
Common Mistakes:
Placing layout.tsx too deep to share with sibling routes
Omitting layout.tsx and expecting shared UI
Misplacing page.tsx files outside correct folders
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.