layout.js
The layout
file is used to define a layout in your Next.js application.
A root layout is the top-most layout in the root app
directory. It is used to define the <html>
and <body>
tags and other globally shared UI.
Reference
Props
children
(required)
Layout components should accept and use a children
prop. During rendering, children
will be populated with the route segments the layout is wrapping. These will primarily be the component of a child Layout (if it exists) or Page, but could also be other special files like Loading or Error when applicable.
params
(optional)
A promise that resolves to an object containing the dynamic route parameters object from the root segment down to that layout.
Example Route | URL | params |
---|---|---|
app/dashboard/[team]/layout.js | /dashboard/1 | Promise<{ team: '1' }> |
app/shop/[tag]/[item]/layout.js | /shop/1/2 | Promise<{ tag: '1', item: '2' }> |
app/blog/[...slug]/layout.js | /blog/1/2 | Promise<{ slug: ['1', '2'] }> |
- Since the
params
prop is a promise. You must useasync/await
or React'suse
function to access the values.- In version 14 and earlier,
params
was a synchronous prop. To help with backwards compatibility, you can still access it synchronously in Next.js 15, but this behavior will be deprecated in the future.
- In version 14 and earlier,
Root Layout
The app
directory must include a root app/layout.js
.
- The root layout must define
<html>
and<body>
tags.- You should not manually add
<head>
tags such as<title>
and<meta>
to root layouts. Instead, you should use the Metadata API which automatically handles advanced requirements such as streaming and de-duplicating<head>
elements.
- You should not manually add
- You can use route groups to create multiple root layouts.
- Navigating across multiple root layouts will cause a full page load (as opposed to a client-side navigation). For example, navigating from
/cart
that usesapp/(shop)/layout.js
to/blog
that usesapp/(marketing)/layout.js
will cause a full page load. This only applies to multiple root layouts.
- Navigating across multiple root layouts will cause a full page load (as opposed to a client-side navigation). For example, navigating from
Caveats
Request Object
Layouts are cached in the client during navigation to avoid unnecessary server requests.
Layouts do not rerender. They can be cached and reused to avoid unnecessary computation when navigating between pages. By restricting layouts from accessing the raw request, Next.js can prevent the execution of potentially slow or expensive user code within the layout, which could negatively impact performance.
To access the request object, you can use headers
and cookies
APIs in Server Components and Functions.
Query params
Layouts do not rerender on navigation, so they cannot access search params which would otherwise become stale.
To access updated query parameters, you can use the Page searchParams
prop, or read them inside a Client Component using the useSearchParams
hook. Since Client Components re-render on navigation, they have access to the latest query parameters.
Pathname
Layouts do not re-render on navigation, so they do not access pathname which would otherwise become stale.
To access the current pathname, you can read it inside a Client Component using the usePathname
hook. Since Client Components re-render during navigation, they have access to the latest pathname.
Fetching Data
Layouts cannot pass data to their children
. However, you can fetch the same data in a route more than once, and use React cache
to dedupe the requests without affecting performance.
Alternatively, when using fetch
in Next.js, requests are automatically deduped.
Accessing child segments
Layouts do not have access to the route segments below itself. To access all route segments, you can use useSelectedLayoutSegment
or useSelectedLayoutSegments
in a Client Component.
Examples
Metadata
You can modify the <head>
HTML elements such as title
and meta
using the metadata
object or generateMetadata
function.
Good to know: You should not manually add
<head>
tags such as<title>
and<meta>
to root layouts. Instead, use the Metadata APIs which automatically handles advanced requirements such as streaming and de-duplicating<head>
elements.
Active Nav Links
You can use the usePathname
hook to determine if a nav link is active.
Since usePathname
is a client hook, you need to extract the nav links into a Client Component, which can be imported into your layout:
Displaying content based on params
Using dynamic route segments, you can display or fetch specific content based on the params
prop.
Reading params
in Client Components
To use params
in a Client Component (which cannot be async
), you can use React's use
function to read the promise:
Version History
Version | Changes |
---|---|
v15.0.0-RC | params is now a promise. A codemod is available. |
v13.0.0 | layout introduced. |