Lesson 3: How to create Layouts and Pages
Last updated
Last updated
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
.
1. Creating Pages:
Next.js uses a file-system routing system. Pages are created by adding files to the app
directory.
Basic Page Creation:
To create a new page, create a new .js
, .jsx
, .ts
, or .tsx
file inside the app
directory.
For example, to create an "about" page, create a file named app/about/page.jsx
(or .tsx
if 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 the app
directory.
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 children
prop, 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 app
directory (app/layout.js
or app/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 from next/link
for 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
, or pnpm 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.js
provides the basic HTML structure, navigation, and footer for the entire application.
Dashboard Layout: The app/dashboard/layout.js
provides a specific layout for the dashboard section, including a sidebar navigation.
Nested Layouts: The Dashboard pages (/dashboard
and /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.