๐ฏ By the end of this lesson you canโฆ
- Create pages and nested routes
- Share UI with layouts
- Navigate with Link
โ๏ธ JSX
app/layout.jsx
๐ Tap a line to explain it
1import "./globals.css";2import Link from "next/link";3 4export const metadata = { title: "My Notes", description: "Notes that think with you" };5 6export default function RootLayout({ children }) {7 return (8 <html lang="en">9 <body>10 <nav>11 <Link href="/">Home</Link> ยท <Link href="/notes">Notes</Link> ยท <Link href="/about">About</Link>12 </nav>13 <main>{children}</main>14 </body>15 </html>16 );17}
โ๏ธ JSX
app/about/page.jsx
1export default function AboutPage() {2 return <h1>About this app</h1>;3}
The same idea, simulated in a live React preview โ click the links and notice the navigation bar never disappears:
โ๏ธ React
App.jsx
1function Layout({ children, go }) {2 return (3 <div style={{ fontFamily: "system-ui" }}>4 <nav style={{ display: "flex", gap: 12, padding: 10, background: "#1f2544", borderRadius: 12 }}>5 {["/", "/notes", "/about"].map((href) => <a key={href} href="#" onClick={(e) => { e.preventDefault(); go(href); }} style={{ color: "#ffd166" }}>{href}</a>)}6 </nav>7 <main style={{ padding: 10 }}>{children}</main>8 </div>9 );10}11const pages = { "/": <h2>๐ Home</h2>, "/notes": <h2>๐ Notes</h2>, "/about": <h2>๐ About</h2> };12function App() {13 const [path, setPath] = useState("/");14 return <Layout go={setPath}>{pages[path]}</Layout>;15}
๐ก Concept check
Why use
<Link> instead of <a> for internal pages?๐ Key takeaways
app/blog/page.jsxโ/blog.layout.jsxwraps every page below it and keeps its state between navigations.<Link href="/about">navigates without a full page reload.
Finished reading & practising?
Saved in this browser. Create a free account to keep it forever.