Next.js 15 Tutorial - 14 - Layouts
Understanding Layouts in Next.js
Introduction to Layouts
- Layouts are UI components shared across multiple pages, providing a consistent structure (e.g., header and footer).
- Creating a layout involves exporting a React component from
layout.jsorlayout.tsx, which accepts achildrenprop for page content.
Default Root Layout
- Every Next.js application must have a root layout file named
layout.tsx, located in the app folder; it is automatically regenerated if deleted.
- The root layout component renders the children prop, which corresponds to the content of
page.tsx.
Enhancing the Layout
- The initial layout can be enhanced by adding HTML elements like headers and footers, using basic styling for simplicity.
- A header with light blue background and footer with white background can be added around the children prop to maintain consistency across routes.
Consistency Across Routes
- The header and footer remain constant regardless of route changes, showcasing the power of the root layout in maintaining application structure.
- Any markup or styles can be added within the layout as long as the children prop is included for rendering unique page content.
Visualizing Layout Functionality
- When visiting different routes (e.g.,
/about,/profile), the corresponding components replace the children content while keeping header and footer intact.