Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

NX
Next.js
0/10 completed
Beginner
Why Next.js?Pages, layouts and routingLinks, images and fonts
Intermediate
Server and Client ComponentsFetching data and streamingRoute Handlers (API routes)Forms and Server Actions
Advanced
Caching and revalidationMetadata, SEO, errors and Proxy
Pro
Auth, deployment and production
Tracks / Next.js / Beginner

Pages, layouts and routing

Next.js uses file-system routing: folders become URL segments, and special files inside them define the UI.

Pages

A page.tsx file makes a route publicly accessible. Default-export a component:

tsx
// app/page.tsx  →  /
export default function Page() {
  return <h1>Hello Next.js!</h1>;
}
tsx
// app/about/page.tsx  →  /about
export default function AboutPage() {
  return <h1>About us</h1>;
}
File URL
app/page.tsx /
app/blog/page.tsx /blog
app/blog/authors/page.tsx /blog/authors

A folder without a page.tsx isn't a route by itself. It just groups the segments below it.

Layouts

A layout is UI shared between pages. It wraps them through the children prop. On navigation, layouts keep their state and don't re-render, which is perfect for headers, sidebars and players.

The root layout is required and must contain <html> and <body>:

tsx
// app/layout.tsx
import './globals.css';

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <header>My Site</header>
        <main>{children}</main>
        <footer>© 2026</footer>
      </body>
    </html>
  );
}

Nested layouts

Layouts nest automatically, following the folder structure:

tsx
// app/dashboard/layout.tsx — wraps /dashboard and everything below it
export default function DashboardLayout({ children }: { children: React.ReactNode }) {
  return (
    <div className="flex">
      <nav className="w-56">Sidebar</nav>
      <section className="flex-1">{children}</section>
    </div>
  );
}
URL /dashboard/settings renders:

<RootLayout>              app/layout.tsx
  <DashboardLayout>       app/dashboard/layout.tsx
    <SettingsPage />      app/dashboard/settings/page.tsx
  </DashboardLayout>
</RootLayout>

Here's the same idea as plain React components you can play with:

Click "Sidebar clicks" a few times, then switch pages: the layout's state is kept. That's exactly how Next.js layouts behave.

Dynamic routes: [slug]

Wrap a folder name in square brackets to capture part of the URL. In Next.js 16, params is a Promise, so await it:

tsx
// app/blog/[slug]/page.tsx  →  /blog/hello-world, /blog/nextjs-tips, …
export default async function BlogPost({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  const post = await getPost(slug);
  return (
    <article>
      <h1>{post.title}</h1>
      <p>{post.body}</p>
    </article>
  );
}

Next.js also provides a global PageProps helper that types params and searchParams from the route, with no import needed:

tsx
export default async function Page(props: PageProps<'/blog/[slug]'>) {
  const { slug } = await props.params;
  return <h1>Post: {slug}</h1>;
}
Folder Matches params
app/blog/[slug]/page.tsx /blog/my-post { slug: 'my-post' }
app/shop/[...slug]/page.tsx /shop/clothes/shirts { slug: ['clothes', 'shirts'] } (catch-all)
app/docs/[[...slug]]/page.tsx /docs and /docs/a/b optional catch-all

Pre-rendering dynamic routes

generateStaticParams tells Next.js which values to build ahead of time:

tsx
// app/blog/[slug]/page.tsx
export async function generateStaticParams() {
  const posts = await getAllPosts();
  return posts.map((post) => ({ slug: post.slug }));
}

Query strings: searchParams

tsx
// app/search/page.tsx  →  /search?q=shoes&page=2
export default async function SearchPage({
  searchParams,
}: {
  searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
}) {
  const { q = '', page = '1' } = await searchParams;
  return <h1>Results for “{q}” (page {page})</h1>;
}

Organising files

Convention Effect
(marketing)/ A route group: it organises files and shares a layout, but is left out of the URL
_components/ A private folder: not routable, a safe place for components and utilities
components/, lib/ outside app/ Also fine; only page and route files create routes
app/
├── (marketing)/
│   ├── layout.tsx      ← marketing layout
│   ├── page.tsx        ← /
│   └── pricing/page.tsx← /pricing
├── (shop)/
│   ├── layout.tsx      ← shop layout with cart
│   └── cart/page.tsx   ← /cart
└── blog/
    ├── _components/PostCard.tsx   ← not a route
    └── [slug]/page.tsx

Special files at a glance

File Purpose
layout.tsx Shared UI that persists across navigation
page.tsx The unique UI of a route
loading.tsx Instant loading UI while the page streams in
error.tsx Error boundary for the segment
not-found.tsx 404 UI
route.ts An API endpoint (Route Handler)
template.tsx Like a layout, but re-mounts on every navigation

Practice (in your project)

  1. Create /about, /blog and /blog/[slug] routes.
  2. Add a blog/layout.tsx with a sidebar listing three post links.
  3. Make the slug page await params and display the slug as a title.
  4. Put the home and about pages in a (marketing) group with its own layout.
✎ Check your understanding

1.Which file makes /pricing publicly accessible?

2.In Next.js 16, how do you read slug in app/blog/[slug]/page.tsx?

← Why Next.js?
React · live preview
Preview