Next.js Tutorial
Next.js is a React framework that enables server-side rendering, static site generation, and other advanced features out of the box. It's widely used for building optimized, production-ready web applications.
1. Setting Up a Next.js Projectâ
Next.js uses create-next-app to quickly set up a project with all necessary configurations.
Step 1: Install create-next-appâ
Run the following command to create a new Next.js application:
npx create-next-app@latest my-next-app
cd my-next-app
Step 2: Start the Development Serverâ
Start the development server to view your app at http://localhost:3000:
npm run dev
2. Pages and Routingâ
In Next.js, each file in the pages directory represents a route. This file-based routing simplifies navigation setup.
Exampleâ
-
Create a new file,
about.js, in thepagesdirectory:export default function About() {
return <h1>About Page</h1>;
} -
Visit
http://localhost:3000/aboutto see the new page.
3. Linking Between Pagesâ
Use Next.jsâs Link component for client-side navigation between pages.
Exampleâ
import Link from 'next/link';
export default function Home() {
return (
<div>
<h1>Home Page</h1>
<Link href="/about">Go to About Page</Link>
</div>
);
}
4. Static and Server-Side Renderingâ
Next.js supports Static Generation and Server-Side Rendering.
Static Generationâ
Create static pages at build time using getStaticProps.
export async function getStaticProps() {
return {
props: { message: "Hello from Static Generation" },
};
}
export default function Home({ message }) {
return <h1>{message}</h1>;
}
Server-Side Renderingâ
Fetch data on each request using getServerSideProps.
export async function getServerSideProps() {
return {
props: { message: "Hello from Server-Side Rendering" },
};
}
export default function Home({ message }) {
return <h1>{message}</h1>;
}
5. API Routesâ
Next.js can handle API routes. Each file in the pages/api directory acts as an API endpoint.
Exampleâ
Create hello.js in pages/api:
export default function handler(req, res) {
res.status(200).json({ message: "Hello from API" });
}
Visit http://localhost:3000/api/hello to see the response.
6. Custom Components and Layoutsâ
To add custom layouts, wrap pages with a layout component.
Example Layoutâ
-
Create
components/Layout.js:export default function Layout({ children }) {
return (
<div>
<header>Header</header>
<main>{children}</main>
<footer>Footer</footer>
</div>
);
} -
Wrap the Home page with the layout:
import Layout from '../components/Layout';
export default function Home() {
return (
<Layout>
<h1>Home Page</h1>
</Layout>
);
}
7. CSS in Next.jsâ
Next.js supports multiple ways to style applications:
- CSS Modules: Scoped styles in
.module.cssfiles. - Global CSS: Global stylesheets added in
_app.js. - Styled Components: CSS-in-JS library.
Example with CSS Modulesâ
-
Create
Home.module.css:.title {
color: blue;
} -
Import and use in your component:
import styles from './Home.module.css';
export default function Home() {
return <h1 className={styles.title}>Home Page</h1>;
}
8. Deploying a Next.js Appâ
Deploying a Next.js app is straightforward, especially with Vercel (Next.js's creators).
- Deploy with Vercel: Sign up at vercel.com, connect your Git repository, and follow the instructions.
- Custom Deployment: Export your project as static files using
next exportor deploy it to any server that supports Node.js.
Summaryâ
This tutorial covered the basics of using Next.js:
- Setting up a Next.js project.
- Working with pages and routing.
- Handling static and server-side rendering.
- Adding API routes and custom layouts.
- Using CSS in Next.js.
Next.js simplifies React development by providing server-side rendering, static generation, and a rich ecosystem of tools for building modern web applications.
Content Reviewâ
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewerâ
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.