Lesson 3: How to create Layouts and Pages
Creating Layouts and Pages
Prerequisites:
Next.js Project: You should have a Next.js project set up. If you haven't already, follow the steps I provided in Lesson 2 to create a new Next.js project using
create-next-app.
Step-by-Step Procedure:
1. Creating Pages:
Next.js uses a file-system routing system. Pages are created by adding files to the
appdirectory.

Basic Page Creation:
To create a new page, create a new
.js,.jsx,.ts, or.tsxfile inside theappdirectory.For example, to create an "about" page, create a file named
app/about/page.jsx(or.tsxif you're using TypeScript).
Page Content:
Inside the page file, you need to export a React component (usually a functional component) as the default export.
Here's an example:
JavaScript
Accessing Pages:
You can access the page in your browser by navigating to the corresponding URL path. For example, the "about" page would be accessible at
http://localhost:3000/about.
2. Creating Layouts:
Layouts are used to share UI components between pages. They wrap your pages and can contain things like headers, footers, navigation menus, etc.

Layout Creation:
To create a layout, create a file named
layout.jsx(or.tsx) inside a directory within theappdirectory.Layouts apply to all pages within the directory and its subdirectories.
For example, to create a layout for a dashboard section, you might create a file at
app/dashboard/layout.jsx.
Layout Content:
Inside the layout file, you need to export a React component as the default export.
Layout components receive a
childrenprop, which represents the page or nested layout being rendered within it.Here's an example:
JavaScript
JavaScript
Root Layout (
app/layout.js):Every Next.js app must have a root layout defined at the top level of the
appdirectory (app/layout.jsorapp/layout.tsx).This layout typically includes the
<html>,<head>, and<body>tags.It's where you should include global styles, metadata, and other elements that are consistent across your entire application.
3. Linking Between Pages:
Next.js provides the
<Link>component fromnext/linkfor client-side navigation between pages.Using
<Link>is important for performance and accessibility. It prevents full page reloads and provides a smoother user experience.Example:
JavaScript
Sample Example: Nested Layouts and Linking Pages
Let's create a scenario with nested layouts and linking:
Project Structure:
Code:
JavaScript
JavaScript
JavaScript
JavaScript
JavaScript
JavaScript
Testing:
Run the development server:
npm run dev,yarn dev, orpnpm dev.Navigate to
http://localhost:3000: You should see the Home page with links.Click the "About Page" link: You should see the About page content, and the navigation and footer from the root layout should be present.
Click the "Dashboard" link: You should see the Dashboard Home page, and both the root layout's navigation/footer and the dashboard layout's sidebar should be present.
Click the "Settings" link in the dashboard sidebar: You should see the Dashboard Settings page, with both layouts still applied.
Explanation of the Example:
Root Layout: The
app/layout.jsprovides the basic HTML structure, navigation, and footer for the entire application.Dashboard Layout: The
app/dashboard/layout.jsprovides a specific layout for the dashboard section, including a sidebar navigation.Nested Layouts: The Dashboard pages (
/dashboardand/dashboard/settings) are wrapped by both the root layout and the dashboard layout, demonstrating nested layouts.Linking: The
<Link>component is used to navigate between pages, providing client-side routing.
By following these steps and the example, you can create and structure layouts and pages in your Next.js application effectively. Remember to organize your files within the app directory to leverage Next.js's file-system routing.
Last updated