Layout
A layout is a structure for consistently managing UI elements that repeat across multiple pages (headers, navigation bars, footers, etc.). By defining a shared layout, you can reduce code duplication and provide a consistent user experience.
Creating a layout file
A layout is _layout.tsx defined by creating a file. The scope it applies to depends on the file's location.
import { PropsWithChildren } from "react";
export default function Layout({ children }: PropsWithChildren) {
return <>{children}</>;
}Setting the layout scope
Layouts are automatically applied according to the file path.
pages/_layout.tsx: Applied globally to all pages.pages/about/_layout.tsx:intoss://{serviceName}/aboutApplied only to all pages under it.
Layouts can be nested. When multiple layouts are used together, they are applied in order starting from the layout in the parent directory.
pages/
├── _layout.tsx // Global layout
├── about/
│ ├── _layout.tsx // about section layout
│ ├── index.tsx // about main page
│ └── team.tsx // team introduction page
└── index.tsx // main pageFor example, if the structure is as shown above, about/team.tsx the page will have layouts applied in the following order.
pages/_layout.tsx(global layout)pages/about/_layout.tsx(section layout)pages/about/team.tsx(actual page component)
With this structure, you can separately manage UI elements needed globally and UI elements needed only in specific sections.
Layout example
Global layout
Let's create a layout that applies to all pages.
Section-specific layout
You can create a layout used only in a specific section.
Getting query parameters from a layout
You can use query parameters in a layout as well. useParams By using the hook, you can read the current screen's parameters and use them dynamically.
useParams Hook usage example
The example below shows the URL query parameter passed in title and displays it as the title at the top of the screen.
Reference docs
Using query parameters
Last updated
Was this helpful?