# The layout.js Convention

> Share UI across pages without destroying state on navigation.

Subject: Next.js | Lesson 6 of 65 | 5 min read
Interactive lesson: https://codewithanimation.in/nextjs/layout-file
Previous: [The page.js Convention](https://codewithanimation.in/nextjs/page-file.md) | Next: [The Root Layout](https://codewithanimation.in/nextjs/root-layout.md)

## What is a Layout?

In Next.js, a `layout.js` (or `.tsx`) file is used to define UI that is shared across multiple pages in a route segment.

If you place a `layout.tsx` file inside an `app/dashboard` folder, its UI (such as a sidebar or header) will automatically wrap every single page nested within the dashboard.

## Persistent state

The true superpower of a layout is that it **does not re-render on navigation**. When a user clicks a link to move from `/dashboard/analytics` to `/dashboard/settings`, Next.js only swaps out the inner page content.

The outer layout stays completely untouched. This means any state inside the layout (like an open accordion, a playing video, or scroll position) is perfectly preserved, resulting in a buttery smooth, app-like user experience.

## The mandatory {children} prop

To create a layout, you export a React component that accepts a `children` prop. You must render this `{children}` prop somewhere in your JSX.

Next.js handles the heavy lifting behind the scenes. When a user visits a URL, Next.js takes the active `page.tsx`, turns it into a React node, and passes it into your layout as the `children` prop.

## Common mistake: The missing children error

A very common beginner mistake is building a beautiful navbar in `layout.tsx`, checking the browser, and realizing the entire webpage is blank below the navbar.

If you forget to include `{children}` in your layout's JSX, the nested pages have nowhere to mount. Next.js literally drops them from the tree, leaving you with a blank screen.

## Tip: Data passing limits

Because Next.js controls the `{children}` prop, you cannot explicitly pass props from a Server Layout down to a Server Page.

If both your layout and your page need the same user data, you shouldn't try to pass it. Instead, just `await getUser()` in both files. Next.js automatically deduplicates `fetch` requests, so the database is only hit once.

## Try it: Layout Persistence

Navigate between the tabs below. Notice how the inner page swaps out, but the outer layout (and its state) stays exactly the same.

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

## Check yourself

### Question 1

What happens to a layout component when a user navigates between two pages that share it?

- A. It unmounts and completely remounts
- B. It fetches fresh data from the server
- C. It does not re-render, preserving its state and scroll position
- D. It converts into a Client Component

Correct answer: C

Layouts persist across navigations. They do not re-render, meaning any active state (like an open dropdown) or scroll position is maintained.

- Why not A: This destroys state, which is what layouts are designed to prevent.
- Why not B: Layouts do not automatically re-fetch data on standard navigation.
- Why not D: Navigation does not change a component's server/client boundaries.

### Question 2

What is the single strict requirement for writing a valid layout.js component?

- A. It must accept and render a 'children' prop
- B. It must use the 'use client' directive
- C. It must fetch data
- D. It must be placed at the root of the app directory

Correct answer: A

Next.js relies on the `children` prop to inject the nested UI (like pages or nested layouts) into the layout wrapper. If you omit it, nothing below it will render.

- Why not B: Layouts can be (and usually are) Server Components.
- Why not C: Data fetching is completely optional.
- Why not D: Layouts can be placed in any route segment folder.

## Remember this

- A `layout.js` shares UI across all routes nested inside its folder.
- Layouts preserve state and do not re-render when navigating between sibling pages.
- A layout MUST accept and render a `{children}` prop.
- Do not try to pass props from a layout to a page; rely on Next.js request memoization to fetch data multiple times efficiently.
