المسارات الديناميكية والمعاملات
تعلّم تنفيذ المسارات الديناميكية للتعامل مع المقاطع المتغيرة في عناوين URL والوصول إلى معاملات المسار
المسارات الديناميكية والمعاملات درس مجاني في Next.js 15 Fullstack (App Router + Server Actions) على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Next.js 15 Fullstack (App Router + Server Actions)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Next.js 15 Fullstack (App Router + Server Actions) 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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.slugis['a']/docs/a/b/c->params.slugis['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
paramsprop in yourpage.jscomponents.
This powerful feature is key for building flexible and scalable web applications!
الأسئلة الشائعة
هل درس «المسارات الديناميكية والمعاملات» مجاني؟
نعم — نص درس «المسارات الديناميكية والمعاملات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Next.js 15 Fullstack (App Router + Server Actions)، انتقل إلى CoddyKit PRO. تتضمن دورة Next.js 15 Fullstack (App Router + Server Actions) 4 دروس في المجموع.
ماذا ستتعلم في «المسارات الديناميكية والمعاملات»؟
تعلّم تنفيذ المسارات الديناميكية للتعامل مع المقاطع المتغيرة في عناوين URL والوصول إلى معاملات المسار تتمرن على Next.js 15 Fullstack (App Router + Server Actions) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Next.js 15 Fullstack (App Router + Server Actions)؟
لا تُشترط خبرة سابقة. Next.js 15 Fullstack (App Router + Server Actions) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «المسارات الديناميكية والمعاملات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Next.js 15 Fullstack (App Router + Server Actions) هذا؟
نعم. كل درس في Next.js 15 Fullstack (App Router + Server Actions) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التخطيطات ومكوّنات الصفحات
- المسارات الديناميكية والمعاملات
- واجهة التحميل والخطأ
- مجموعات المسارات والتخطيطات المتداخلة