# Route Segments & Nested Routing

> How URLs translate into layers of nested UI.

Subject: Next.js | Lesson 3 of 65 | 5 min read
Interactive lesson: https://codewithanimation.in/nextjs/route-segments-nested
Previous: [App Router File-System Routing](https://codewithanimation.in/nextjs/app-router-routing.md) | Next: [Server Component Payloads & Serialization](https://codewithanimation.in/nextjs/rsc-payloads.md)

## Segments and URLs

In web development, a URL is made up of route segments delimited by slashes. For example, in the URL `/dashboard/settings`, `dashboard` is a segment and `settings` is a nested segment.

In the Next.js App Router, these URL segments map directly to your folder structure. When you nest a `settings` folder inside a `dashboard` folder, you create a nested route.

## Nested Routes = Nested UI

Nested routing isn't just about organizing files; it fundamentally controls how your UI is rendered. Next.js uses nested routes to automatically create nested UI layouts.

When a user navigates deeper into a URL, they aren't loading a completely new page. Instead, they are loading a smaller chunk of UI that fits inside the parent route's layout.

## The {children} prop

If you have a `layout.tsx` file in your `/dashboard` folder, it acts as a wrapper. Any route nested inside `/dashboard` (like `/dashboard/settings`) will automatically be passed into that layout as the `children` prop.

This means your outer layout (like a sidebar or navigation bar) stays perfectly preserved and does not re-render when the user navigates between nested route segments.

## Common mistake: The 'Full Page Replace' illusion

A common beginner mistake is assuming that navigating to a nested route will clear the screen and render a completely new page.

If you put a global navigation bar inside `app/layout.tsx`, it will persist across every single route in your application. A nested `page.tsx` never replaces its parent layouts; it only fills the empty space (the `{children}`) left by them.

## Tip: Invisible Route Segments

Sometimes you want to group routes logically in your folder structure, or share a layout, without changing the actual URL. You can do this using Route Groups.

By wrapping a folder name in parentheses, like `(marketing)`, Next.js will ignore it when building the URL path. A file at `app/(marketing)/about/page.tsx` will be accessible at just `/about`.

## Try it: Interactive Route Builder

Add URL segments below and watch how Next.js wraps the UI in corresponding layouts.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/route-segments-nested

## Check yourself

### Question 1

How does a nested route segment (like /settings inside /dashboard) affect the UI?

- A. It forces the entire page to reload
- B. It replaces the parent layout entirely
- C. It renders its page.tsx inside the parent layout's children prop
- D. It throws an error unless configured in next.config.js

Correct answer: C

Nested folders create nested UI. The `page.tsx` of a nested route is injected into the `children` prop of the closest parent `layout.tsx`.

- Why not A: Next.js does client-side transitions, avoiding full page reloads.
- Why not B: Nested routes render inside parent layouts, they don't replace them.
- Why not D: Nested routing is automatic in the App Router.

### Question 2

If you have a file at `app/(admin)/users/page.tsx`, what is the correct URL to access it?

- A. /(admin)/users
- B. /admin/users
- C. /users
- D. It cannot be accessed directly

Correct answer: C

Folders wrapped in parentheses `()` are Route Groups. They do not affect the URL path, meaning `(admin)` is bypassed, leaving just `/users`.

- Why not A: Parentheses tell the router to omit the folder from the URL.
- Why not B: The parentheses make the segment invisible to the URL.
- Why not D: It is perfectly accessible, just without the route group in the URL.

## Remember this

- A URL is made up of segments that match your nested folder structure.
- Nested routes automatically result in nested UI layouts.
- A nested `page.tsx` never replaces its parent layouts; it renders inside them.
- Wrap a folder name in parentheses `(folderName)` to create a Route Group that doesn't affect the URL.
