Getting Started with Next.js: A Comprehensive Guide
Getting Started with Next.js: A Comprehensive Guide
Next.js has become one of the most popular React frameworks for building modern web applications. In this comprehensive guide, we'll explore what makes Next.js special and how to get started with your first project.
What is Next.js?
Next.js is a React framework that provides a set of tools and conventions for building production-ready web applications. It offers features like:
- Server-Side Rendering (SSR) - Render pages on the server for better SEO and performance
- Static Site Generation (SSG) - Pre-render pages at build time
- API Routes - Build API endpoints within your Next.js application
- Automatic Code Splitting - Only load the JavaScript needed for each page
- Built-in CSS Support - Support for CSS Modules, Sass, and CSS-in-JS
Setting Up Your First Next.js Project
Getting started with Next.js is incredibly simple. You can create a new project using the create-next-app
command:
npx create-next-app@latest my-app --typescript --tailwind --eslint
cd my-app
npm run dev
This command creates a new Next.js project with TypeScript, Tailwind CSS, and ESLint configured out of the box.
Project Structure
A typical Next.js project has the following structure:
my-app/
├── src/
│ └── app/
│ ├── layout.tsx
│ ├── page.tsx
│ └── globals.css
├── public/
├── package.json
└── next.config.js
src/app/
- Contains your application pages and layouts (App Router)public/
- Static assets like images and iconsnext.config.js
- Configuration file for Next.js
Creating Your First Page
With the App Router (Next.js 13+), creating pages is as simple as adding files to the src/app
directory:
// src/app/about/page.tsx
export default function About() {
return (
<div>
<h1>About Us</h1>
<p>Welcome to our about page!</p>
</div>
);
}
This automatically creates a route at /about
.
Adding Dynamic Routes
Next.js supports dynamic routes using square brackets in file names:
// src/app/blog/[slug]/page.tsx
interface BlogPostProps {
params: { slug: string };
}
export default function BlogPost({ params }: BlogPostProps) {
return (
<div>
<h1>Blog Post: {params.slug}</h1>
</div>
);
}
Styling Your Application
Next.js supports various styling approaches:
CSS Modules
import styles from './page.module.css';
export default function Home() {
return <div className={styles.container}>Hello World</div>;
}
Tailwind CSS
export default function Home() {
return (
<div className="flex items-center justify-center min-h-screen bg-gray-100">
<h1 className="text-4xl font-bold text-blue-600">Hello World</h1>
</div>
);
}
Data Fetching
Next.js provides several ways to fetch data:
Server Components (Default)
async function getData() {
const res = await fetch('https://api.example.com/data');
return res.json();
}
export default async function Page() {
const data = await getData();
return <div>{data.title}</div>;
}
Client Components
'use client';
import { useState, useEffect } from 'react';
export default function ClientPage() {
const [data, setData] = useState(null);
useEffect(() => {
fetch('/api/data')
.then(res => res.json())
.then(setData);
}, []);
return <div>{data?.title}</div>;
}
Deployment
Next.js applications can be deployed to various platforms:
- Vercel (recommended) - Zero-configuration deployment
- Netlify - Great for static sites
- AWS - Using services like Amplify or EC2
- Docker - Containerized deployment
For Vercel deployment:
npm install -g vercel
vercel
Conclusion
Next.js provides an excellent foundation for building modern web applications. Its combination of performance optimizations, developer experience, and deployment simplicity makes it a great choice for projects of any size.
Start with a simple project and gradually explore more advanced features like middleware, internationalization, and advanced routing patterns.
Happy coding!