Next.js has become the go-to framework for building modern React applications. With its powerful features like server-side rendering, static site generation, and API routes, it's no wonder why developers love it.
Next.js is a React framework that provides an excellent developer experience with features like:
Make sure you have Node.js installed (version 16.14 or later).
npx create-next-app@latest my-nextjs-app
cd my-nextjs-app
npm run dev
my-nextjs-app/
├── app/ # App Router (Next.js 13+)
│ ├── page.tsx # Home page
│ ├── layout.tsx # Root layout
│ └── globals.css # Global styles
├── public/ # Static assets
├── components/ # Reusable components
└── package.json
Next.js 13+ introduced the App Router, which uses React Server Components by default.
// app/page.tsx
export default function HomePage() {
return (
<main>
<h1>Welcome to My Next.js App</h1>
</main>
)
}
// app/layout.tsx
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
app/page.tsx → /app/about/page.tsx → /aboutapp/blog/[slug]/page.tsx → /blog/any-slug// app/blog/[slug]/page.tsx
export default function BlogPost({ params }: { params: { slug: string } }) {
return (
<article>
<h1>Blog Post: {params.slug}</h1>
</article>
)
}
// app/posts/page.tsx
async function getPosts() {
const res = await fetch('https://api.example.com/posts')
return res.json()
}
export default async function PostsPage() {
const posts = await getPosts()
return (
<div>
{posts.map((post: any) => (
<div key={post.id}>{post.title}</div>
))}
</div>
)
}
'use client'
import { useState, useEffect } from 'react'
export default function ClientComponent() {
const [data, setData] = useState(null)
useEffect(() => {
fetch('/api/data')
.then(res => res.json())
.then(setData)
}, [])
return <div>{/* render data */}</div>
}
Create backend functionality with API routes:
// app/api/posts/route.ts
import { NextResponse } from 'next/server'
export async function GET() {
const posts = [
{ id: 1, title: 'First Post' },
{ id: 2, title: 'Second Post' }
]
return NextResponse.json(posts)
}
export async function POST(request: Request) {
const body = await request.json()
// Handle creating a new post
return NextResponse.json({ message: 'Post created' })
}
/* styles/Button.module.css */
.button {
padding: 10px 20px;
background: blue;
color: white;
}
import styles from './Button.module.css'
export default function Button() {
return <button className={styles.button}>Click me</button>
}
export default function Button() {
return (
<button className="px-4 py-2 bg-blue-500 text-white rounded">
Click me
</button>
)
}
npm install -g vercel
vercel
Next.js provides an excellent foundation for building modern web applications. Its combination of performance, developer experience, and flexibility makes it an ideal choice for both small projects and large-scale applications.
Start building with Next.js today and experience the future of React development!