Skip to content

Folders Are Routes

The Course
•
20 min study

In a React app from the earlier courses, there was one page. To show different screens, you changed state. In Next.js, each address, or route, is its own page, and you make one by making a folder.

Routes from folders Next.js · 7 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header>Study Hub</header> {children} </body> </html> ); } import Greeting from "./Greeting.jsx"; export default function HomePage() { return ( <main> <h1>Study Hub</h1> <Greeting name="Ada" /> <p className="muted">Try /about, /topics and /topics/react in the address bar.</p> </main> ); } // A component, not a page: only files named page.jsx become routes. export default function Greeting({ name }) { return <p>Welcome back, {name}.</p>; } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } header, main, footer { max-width: 36rem; margin: 0 auto; padding: 0.75rem 1rem; } header { border-bottom: 1px solid #d6dde6; } nav { display: flex; gap: 1rem; } a { color: #06617f; } h1 { margin: 0.5rem 0; } .muted { color: #5b6876; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } export default function AboutPage() { return ( <main> <h1>About</h1> <p>A place to plan what you study each day.</p> </main> ); } export default function TopicsPage() { return ( <main> <h1>Topics</h1> <p>React, Next.js and CSS.</p> </main> ); } export default function ReactTopicPage() { return ( <main> <h1>React</h1> <p>Components, props and state.</p> </main> ); }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.

Type /about into the preview's address bar and press Enter. Then try /topics and /topics/react.

The rule

A folder inside app/ adds a part to the address. A file named page.jsx inside it makes that address a page.

FileRoute
app/page.jsx/
app/about/page.jsx/about
app/topics/page.jsx/topics
app/topics/react/page.jsx/topics/react

Nested folders make longer addresses. /topics and /topics/react are separate pages: each folder has its own page.jsx.

Not every file is a page

app/Greeting.jsx is in app/ too, but it's an ordinary component that app/page.jsx imports. Only files named page.jsx become routes, so you can keep components next to the pages that use them:

JSX
export default function Greeting({ name }) {
  return <p>Welcome back, {name}.</p>;
}

Many projects keep shared components in a components/ folder beside app/ instead. Both work; this course keeps small ones close to their page.

Predict

What does /Greeting show?

Check your prediction

404 · This page could not be found. There's no app/greeting/page.jsx, so there's no route. That's Next.js's built-in "not found" page, inside your root layout: the Study Hub header is still there. You'll replace it with your own in this day's fourth lesson.

Exercise

A CSS topic

Add a page at /topics/css with the heading CSS and the text Selectors, the box model and layout.

Your working space Next.js · 7 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header>Study Hub</header> {children} </body> </html> ); } import Greeting from "./Greeting.jsx"; export default function HomePage() { return ( <main> <h1>Study Hub</h1> <Greeting name="Ada" /> <p className="muted">Try /about, /topics and /topics/react in the address bar.</p> </main> ); } // A component, not a page: only files named page.jsx become routes. export default function Greeting({ name }) { return <p>Welcome back, {name}.</p>; } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } header, main, footer { max-width: 36rem; margin: 0 auto; padding: 0.75rem 1rem; } header { border-bottom: 1px solid #d6dde6; } nav { display: flex; gap: 1rem; } a { color: #06617f; } h1 { margin: 0.5rem 0; } .muted { color: #5b6876; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } export default function AboutPage() { return ( <main> <h1>About</h1> <p>A place to plan what you study each day.</p> </main> ); } export default function TopicsPage() { return ( <main> <h1>Topics</h1> <p>React, Next.js and CSS.</p> </main> ); } export default function ReactTopicPage() { return ( <main> <h1>React</h1> <p>Components, props and state.</p> </main> ); }
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Hint 1: the idea

Its address is /topics/css, so the file goes in app/topics/css/. In the File path field under the lab, enter app/topics/css/page.jsx and press Add file, then write its default-exported component.

Hint 2: a first step

Copy app/topics/react/page.jsx and change the component's name and text.

Show one solution

app/topics/css/page.jsx:

JSX
export default function CssTopicPage() {
  return (
    <main>
      <h1>CSS</h1>
      <p>Selectors, the box model and layout.</p>
    </main>
  );
}

Was this article helpful?

Your response is saved on this device.