Build a Simple Next.js App Using the App Directory (App Router)
📖 Scenario: You are creating a small website homepage using Next.js with the new App Router feature. This new way organizes pages inside the app folder instead of the old pages folder.The website will show a welcome message and a list of three favorite fruits.
🎯 Goal: Build a Next.js app using the app directory. Create a homepage that displays a heading and a list of fruits using React components and the App Router conventions.
📋 What You'll Learn
Create an app folder with a page.tsx file
Define a React functional component named HomePage inside page.tsx
Use a constant array fruits with exactly these strings: "Apple", "Banana", "Cherry"
Render a heading <h1> with the text "Welcome to My Fruit Site"
Render an unordered list <ul> showing each fruit from the fruits array as a list item <li>
Export the HomePage component as the default export
💡 Why This Matters
🌍 Real World
Next.js App Router is the modern way to build React apps with file-based routing and server components, used in many professional web projects.
💼 Career
Understanding the app directory and how to create pages with React components is essential for frontend developers working with Next.js in real-world jobs.
Progress0 / 4 steps
1
Create the fruits array
Inside the app/page.tsx file, create a constant array called fruits with these exact strings: "Apple", "Banana", and "Cherry".
NextJS
Hint
Use const fruits = ["Apple", "Banana", "Cherry"] to create the array.
2
Create the HomePage component
Add a React functional component named HomePage that returns a fragment with an <h1> heading containing the text "Welcome to My Fruit Site".
NextJS
Hint
Define function HomePage() { return ( ... ) } and inside return an <h1> with the welcome text.
3
Render the fruits list inside HomePage
Inside the HomePage component's return, add an unordered list <ul>. Use fruits.map with fruit as the iterator variable to create a list item <li> for each fruit. Each <li> should have a key attribute set to the fruit string.
NextJS
Hint
Use {fruits.map(fruit => (<li key={fruit}>{fruit}</li>))} inside the <ul>.
4
Export the HomePage component as default
Add a default export statement for the HomePage component at the end of page.tsx.
NextJS
Hint
Write export default HomePage at the end of the file.
Practice
(1/5)
1. In Next.js App Router, what does a folder containing a page.js file represent?
easy
A. A route in the application
B. A layout wrapper for pages
C. A server API endpoint
D. A static asset folder
Solution
Step 1: Understand the role of page.js in App Router
In Next.js App Router, each folder with a page.js file defines a route that users can visit.
Step 2: Differentiate from other files
Files like layout.js define layouts, not routes. API routes are in api/ folders, and static assets are in public/.
Final Answer:
A route in the application -> Option A
Quick 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
A. Create a layout.js file inside the route folder
B. Create a page.js file inside the route folder
C. Create a _app.js file in the root folder
D. Create a server.js file in the route folder
Solution
Step 1: Identify layout file naming in App Router
Layouts are defined by creating a layout.js file inside the folder representing the route.
Step 2: Exclude old or unrelated files
The _app.js file is used in the old pages router, not App Router. page.js defines pages, not layouts. server.js is unrelated.
Final Answer:
Create a layout.js file inside the route folder -> Option A
Quick 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 app/ directory: