Nobody types addresses to get around a site. They follow links.
Links and nested layoutsNext.js · 8 filesimport Link from "next/link";
import "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<nav>
<Link href="/">Home</Link>
<Link href="/topics">Topics</Link>
<Link href="/about">About</Link>
</nav>
</header>
{children}
</body>
</html>
);
}
import Link from "next/link";
export default function HomePage() {
return (
<main>
<h1>Study Hub</h1>
<p>
Start with the <Link href="/topics/react">React topic</Link>.
</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;
}
export default function AboutPage() {
return (
<main>
<h1>About</h1>
<p>A place to plan what you study each day.</p>
</main>
);
}
import Link from "next/link";
// Wraps every page under /topics, inside the root layout.
export default function TopicsLayout({ children }) {
return (
<main>
<p className="muted">
Topics: <Link href="/topics/react">React</Link> · <Link href="/topics/nextjs">Next.js</Link>
</p>
{children}
</main>
);
}
export default function TopicsPage() {
return <h1>Topics</h1>;
}
export default function NextTopicPage() {
return (
<>
<h1>Next.js</h1>
<p>Pages, layouts and the server.</p>
</>
);
}
export default function ReactTopicPage() {
return (
<>
<h1>React</h1>
<p>Components, props and state.</p>
</>
);
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Follow React topic, then Next.js, then About. Watch the address bar,
then press its back button.
Link
JSX
import Link from "next/link";<Link href="/topics/react">React topic</Link>
Link renders an ordinary <a>, so it works with the keyboard, opens in a new
tab, and can be read by screen readers. When it's clicked, Next.js swaps only
the page that changed, without reloading the whole site, and keeps the browser's
back and forward buttons working.
Link, not a
On a real Next.js site, a plain <a href="/about"> makes the browser load the
whole site again from scratch. In these labs, the two behave the same, so the
difference won't show here. Use Link for pages in your own site, and <a> for
other sites.
Layouts nest
Two layouts are at work here:
app/layout.jsx, the root layout, has the main navigation. It wraps every
page:
You're on React and follow Next.js. Which of the three components,
RootLayout, TopicsLayout and the page, does Next.js replace?
Check your prediction
Only the page. Both layouts stay on screen, as the same elements: they aren't
built again. That's what makes layouts cheap and useful. On Day 2, you'll see
that this also means anything a layout remembers survives the move.
Exercise
A footer and a way back
Add a footer that says Made while learning Next.js. to every page.
Add an All topics link to the start of the topics menu, going to
/topics.
Your working spaceNext.js · 8 filesimport Link from "next/link";
import "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<nav>
<Link href="/">Home</Link>
<Link href="/topics">Topics</Link>
<Link href="/about">About</Link>
</nav>
</header>
{children}
</body>
</html>
);
}
import Link from "next/link";
export default function HomePage() {
return (
<main>
<h1>Study Hub</h1>
<p>
Start with the <Link href="/topics/react">React topic</Link>.
</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;
}
export default function AboutPage() {
return (
<main>
<h1>About</h1>
<p>A place to plan what you study each day.</p>
</main>
);
}
import Link from "next/link";
// Wraps every page under /topics, inside the root layout.
export default function TopicsLayout({ children }) {
return (
<main>
<p className="muted">
Topics: <Link href="/topics/react">React</Link> · <Link href="/topics/nextjs">Next.js</Link>
</p>
{children}
</main>
);
}
export default function TopicsPage() {
return <h1>Topics</h1>;
}
export default function NextTopicPage() {
return (
<>
<h1>Next.js</h1>
<p>Pages, layouts and the server.</p>
</>
);
}
export default function ReactTopicPage() {
return (
<>
<h1>React</h1>
<p>Components, props and state.</p>
</>
);
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
What wraps every page? Put the footer there, after {children}.
Hint 2: a first step
It's in app/topics/layout.jsx. Replace the word Topics: with a Link.
Show one solution
In app/layout.jsx, after {children}:
JSX
<footer className="muted">Made while learning Next.js.</footer>