Nextjs Fundamentals · App Router

Pages and layouts

file-system routing (`page`, `layout`)

Pages and layouts

Next.js uses file-system based routing: folders and files in the app directory define the routes of your site. A folder is a route segment that maps to a URL segment, and a special file inside it renders the UI for that segment.

A page is the UI for one route. Add a page file and default-export a React component:

// app/page.tsx
export default function Page() {
  return <h1>Hello Next.js!</h1>;
}

A layout is UI shared between multiple pages. Default-export a component that accepts a children prop and render children where the page should appear:

// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

The layout at the root of app is the root layout. It is required, and it must contain the <html> and <body> tags. On navigation, layouts preserve their state, stay interactive, and do not rerender.

In short: folders define the routes, a page renders one route, and a layout wraps pages with shared chrome.

You are about to answer a short quiz on file-system routing (page, layout).

  1. In the App Router, a `page` file default-exports the UI for a route.
  2. A layout renders the page or child layout below it through its ___ prop.
  3. The root layout is required and must contain the `<html>` and `<body>` tags.
  4. A layout rerenders from scratch every time you navigate between its child pages.
  5. In the App Router, folders define the route ___ that map to URL segments.