DEV Community

Jeet Bhalani
Jeet Bhalani

Posted on

1 1 1

Getting Started with Next.js: A Simple Guide

Next.js is a powerful React framework that simplifies building fast, scalable web applications. With built-in features like server-side rendering (SSR), static site generation (SSG), and API routes, it provides a great developer experience.

In this quick guide, we’ll set up a Next.js project, explore its core features, and build a simple page.

πŸ“Œ Prerequisites

βœ… Node.js installed (recommended: v18+)
βœ… Basic knowledge of React.js

πŸš€ Step 1: Setting Up a Next.js Project

To create a new Next.js project, run the following command in your terminal:

npx create-next-app@latest my-next-app
cd my-next-app
npm run dev
Enter fullscreen mode Exit fullscreen mode

This starts a development server at http://localhost:3000. πŸŽ‰

πŸ“‚ Step 2: Understanding the Project Structure

A new Next.js project includes:

πŸ“ pages/ – Each file here becomes a route (e.g., index.js β†’ /)
πŸ“ public/ – Stores static assets (images, icons, etc.)
πŸ“ styles/ – Contains global styles and CSS modules

πŸ› οΈ Step 3: Creating a Page

Each file inside the pages/ directory automatically becomes a route. Let’s create an About page:

// pages/about.js
export default function About() {
  return <h1>About Page</h1>;
}
Enter fullscreen mode Exit fullscreen mode

Visit http://localhost:3000/about to see it in action!

🎨 Step 4: Styling in Next.js.

Next.js supports global CSS, CSS Modules, and Tailwind CSS.

Using CSS Modules:

Create a CSS file inside the styles/ directory:

/* styles/Home.module.css */
.title {
  color: blue;
  font-size: 24px;
}
Enter fullscreen mode Exit fullscreen mode

Import it inside a page:

import styles from '../styles/Home.module.css';

export default function Home() {
  return <h1 className={styles.title}>Hello Next.js</h1>;
}
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Step 5: Fetching Data in Next.js

Next.js provides multiple ways to fetch data:

  • getStaticProps (SSG) – Fetches data at build time
  • getServerSideProps (SSR) – Fetches data on every request
  • useEffect (CSR) – Fetches data on the client side
  • Example using getStaticProps:
export async function getStaticProps() {
  const res = await fetch('https://jsonplaceholder.typicode.com/posts');
  const posts = await res.json();

  return {
    props: { posts },
  };
}
Enter fullscreen mode Exit fullscreen mode

This method pre-renders the page with fetched data.

πŸš€ Step 6: Deploying Next.js

You can deploy your Next.js app easily using Vercel (the creators of Next.js).

To deploy:

  • Push your project to GitHub
  • Go to Vercel and import your repo
  • Click Deploy πŸŽ‰

🎯 Final Thoughts

Next.js is an excellent choice for modern web development, offering SEO benefits, fast performance, and easy routing. Start building your first project today!

πŸš€ What’s next? Explore:

πŸ”Ή API Routes in Next.js
πŸ”Ή Middleware & Authentication
πŸ”Ή Server Actions & Optimizations.

Happy coding! πŸ’»βœ¨

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more