Skip to content

Hello, Next.js

The Course
•
15 min study

What Next.js adds

React builds the interface: components, props and state. A real website needs more around it:

  • many pages, each with its own address;
  • a server to load data and save changes;
  • a way to make it all fast to load.

Next.js is a framework built on React that provides those. You still write React components, but where a file lives decides its address, and a component can run on the server, not just in the browser.

This course uses Next.js's App Router: the app/ folder. It assumes React components, props, state, events and controlled forms. If those are unfamiliar, start with React beginner.

The labs simulate a Next.js server inside your browser. They are not a hosted Next.js deployment, and every project file is visible to the reader. Use only practice data in them. Day 7 runs the downloaded project with real Next.js.

Your first project

Hello, Next.js Next.js · 3 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header>Study Hub</header> {children} </body> </html> ); } export default function HomePage() { console.log("Rendering the home page"); return ( <main> <h1>Hello, Next.js</h1> <p>This page is the file app/page.jsx.</p> </main> ); } 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; }
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.

There are three files:

FileWhat it is
app/page.jsxThe page at /, the home page
app/layout.jsxThe root layout: it wraps every page
app/globals.cssStyles for the whole site, imported by the layout

The preview has an address bar, like a small browser. It says /, the page you're on. It navigates inside this lab, not the learning website.

When an exercise asks for a new file, enter its full relative path in File path under the lab, press Add file, then write its code in the editor. To remove a file, enter its path and press Remove file. Restore starter files brings back the original project and replaces your edits; download work you want to keep first. Downloads include your added files.

The page

JSX
export default function HomePage() {
  console.log("Rendering the home page");
  return (
    <main>
      <h1>Hello, Next.js</h1>
      <p>This page is the file app/page.jsx.</p>
    </main>
  );
}

A page is an ordinary React component, the file's default export. Next.js finds it by its file name: you never import it or render it yourself.

The layout

JSX
export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <header>Study Hub</header>
        {children}
      </body>
    </html>
  );
}

The layout gets the current page as children. Whatever it renders around children, here a header, appears on every page. The root layout is the only component that renders <html> and <body>; nothing else in the project should.

Predict

The page calls console.log. Look at the console under the preview: what's different about that line from the logs in the React courses?

Check your prediction

It starts with [server]. Pages run on the server by default, not in the browser. In this lab, a worker inside the preview plays the server. When you run Next.js on your own computer, the line appears in the terminal where the server runs. Development tooling can also forward server logs to browser devtools; seeing a log there doesn't prove its code ran in the browser. Day 2 explains the rendering boundary.

Exercise

Make it yours

Change the header to your own site's name, and add a second paragraph to the page that says what you want to learn.

Your working space Next.js · 3 files
import "./globals.css"; export default function RootLayout({ children }) { return ( <html lang="en"> <body> <header>Study Hub</header> {children} </body> </html> ); } export default function HomePage() { console.log("Rendering the home page"); return ( <main> <h1>Hello, Next.js</h1> <p>This page is the file app/page.jsx.</p> </main> ); } 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; }
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

Shared content belongs in a layout; content for one URL belongs in its page. Decide which file owns each change.

Hint 2: a first step

The header is in app/layout.jsx; the paragraph goes inside <main> in app/page.jsx. Press Run after editing.

Show one solution

In app/layout.jsx:

JSX
<header>Ada's Study Hub</header>

In app/page.jsx, under the first paragraph:

JSX
<p>I want to learn how pages load data.</p>

Was this article helpful?

Your response is saved on this device.