Next.js 15 Fullstack (App Router + Server Actions) · Lekcja

Trasy dynamiczne i parametry

Naucz się implementować dynamiczne trasy obsługujące zmienne segmenty adresów URL i uzyskiwać dostęp do parametrów tras

Lekcja 2 z 411 kroki

Trasy dynamiczne i parametry to bezpłatna lekcja Next.js 15 Fullstack (App Router + Server Actions) na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Next.js 15 Fullstack (App Router + Server Actions), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Next.js 15 Fullstack (App Router + Server Actions) zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

What are Dynamic Routes?

Imagine you have a blog with many posts. Instead of creating a separate page for each post (like /blog/post-1, /blog/post-2), you can use dynamic routes.

Dynamic routes allow you to create pages that respond to variable segments in the URL, making your application scalable and maintainable.

Defining a Basic Dynamic Route

In Next.js App Router, you define a dynamic route segment by wrapping a folder name in square brackets []. For example, to create a dynamic route for blog posts, you'd create a folder named [slug].

  • app/blog/[slug]/page.js

This structure will match URLs like /blog/first-post, /blog/another-article, etc.

Accessing Route Parameters

When a dynamic route is matched, the variable part of the URL becomes available as a route parameter. You can access these parameters within your page component via the params prop.

For app/blog/[slug]/page.js, the URL /blog/my-first-post would make params.slug equal to 'my-first-post'.

Demo: Simple Dynamic Page

Let's see how to create a simple dynamic page that displays the slug parameter from the URL. Place this code in app/blog/[slug]/page.js.

export default function BlogPostPage({ params }) {
  // params.slug will contain the dynamic part of the URL
  return (
    <div>
      <h1>Blog Post: {params.slug}</h1>
      <p>This content is dynamically loaded!</p>
    </div>
  );
}

Understanding Catch-all Segments

What if you need to match a path with an unknown number of segments? This is where catch-all segments come in handy. You define them using a spread syntax: [...slug].

  • app/docs/[...slug]/page.js

This will match URLs like /docs/a, /docs/a/b, /docs/a/b/c, etc.

Accessing Catch-all Parameters

When using a catch-all segment, the params prop will contain an array of the matched segments. So, for app/docs/[...slug]/page.js:

  • /docs/a -> params.slug is ['a']
  • /docs/a/b/c -> params.slug is ['a', 'b', 'c']

Demo: Catch-all Page

Here's how you can display all segments from a catch-all route. Place this code in app/docs/[...slug]/page.js.

export default function DocsPage({ params }) {
  // params.slug is an array of path segments
  const path = params.slug.join('/');
  return (
    <div>
      <h1>Documentation Path: /{path}</h1>
      <p>Showing content for: {path}</p>
    </div>
  );
}

Optional Catch-all Segments

Sometimes you want a catch-all segment that can also match the base path (i.e., zero segments). This is an optional catch-all, defined with double square brackets: [[...slug]].

  • app/users/[[...id]]/page.js

This will match /users (where params.id is undefined) AND /users/123 (where params.id is ['123']).

Demo: Optional Catch-all

This example shows an optional catch-all route that displays a user ID or 'Guest' if no ID is provided. Place this in app/users/[[...id]]/page.js.

export default function UserProfilePage({ params }) {
  // params.id will be undefined for /users, or ['123'] for /users/123
  const userId = params.id ? params.id[0] : 'Guest';
  return (
    <div>
      <h1>Welcome, {userId}</h1>
      <p>This page handles both base and dynamic paths.</p>
    </div>
  );
}

Dynamic Routes Quiz

Which of the following file structures would allow a Next.js App Router application to handle URLs like /products/electronics/laptops and access 'electronics' and 'laptops' separately?

Recap: Dynamic Routes & Params

Great job! You've learned the essentials of dynamic routing in Next.js App Router:

  • [slug] creates a single dynamic segment.
  • [...slug] creates a catch-all segment for multiple paths, returning an array.
  • [[...slug]] creates an optional catch-all, matching zero or more segments.
  • All dynamic segments are accessed via the params prop in your page.js components.

This powerful feature is key for building flexible and scalable web applications!

Bezpłatny start

Ucz się TypeScript dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
22
Lekcje
88

Często zadawane pytania

Czy lekcja „Trasy dynamiczne i parametry” jest bezpłatna?

Tak — pełny tekst „Trasy dynamiczne i parametry” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Next.js 15 Fullstack (App Router + Server Actions), przejdź na CoddyKit PRO. Kurs Next.js 15 Fullstack (App Router + Server Actions) zawiera 4 lekcji w sumie.

Co nauczysz się w „Trasy dynamiczne i parametry”?

Naucz się implementować dynamiczne trasy obsługujące zmienne segmenty adresów URL i uzyskiwać dostęp do parametrów tras Ćwiczysz Next.js 15 Fullstack (App Router + Server Actions) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Next.js 15 Fullstack (App Router + Server Actions)?

Nie wymagamy żadnego doświadczenia. Next.js 15 Fullstack (App Router + Server Actions) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „Trasy dynamiczne i parametry”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Next.js 15 Fullstack (App Router + Server Actions)?

Tak. Każda lekcja Next.js 15 Fullstack (App Router + Server Actions) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Layouty i komponenty stron
  2. Trasy dynamiczne i parametry
  3. Interfejs ładowania i błędów
  4. Grupy tras i zagnieżdżone layouty
← Powrót do Next.js 15 Fullstack (App Router + Server Actions)