Getting Started with Next.js: A Beginner’s Guide

@rnab
3 min readFeb 10, 2025

--

Welcome to a journey into one of the most powerful frameworks in the React ecosystem, Next.js. If you’re looking to supercharge your React applications and propel your web development skills to the next level, you’re in the right place. In this guide, we’ll introduce you to Next.js, walk through its core concepts, and provide code examples in TypeScript to help cement your understanding.

What is Next.js?

Next.js is a React framework that provides a multitude of features for building server-rendered React applications. It offers functionalities like static-site generation, server-side rendering, API routes, and built-in CSS support, to name just a few. With Next.js, you can build fast, responsive applications with improved SEO and pre-rendered content, all while maintaining the flexibility and composability of React components.

Why Next.js?

  1. Server-Side Rendering (SSR): Next.js allows you to render pages on the server before sending them to the client. This improves performance and SEO.
  2. Static Site Generation (SSG): Ideal for pages that don’t change often. You can generate HTML at build time.
  3. API Routes: Let you build and deploy serverless functions as API endpoints.
  4. Automatic Code Splitting: Improves performance by loading only the necessary JavaScript for each page.
  5. Zero-Config: Minimal setup required to get started, allowing you to focus more on building your app.

Now, let’s dive into setting up a Next.js project with TypeScript, step by step.

Setting Up a New Next.js Project

Before diving in, ensure you have Node.js installed on your system. We will use npx to create a Next.js app:

npx create-next-app@latest my-nextjs-app --typescript

This command sets up a new Next.js project named my-nextjs-app with TypeScript support.

Understanding the Project Structure

Once your project is successfully created, let’s go through the significant directories and files:

  • pages/: Contains all the page components. Any .tsx file in this directory automatically becomes a route.
  • public/: Stores static assets like images, which can be served directly.
  • styles/: Default directory for styling files (CSS).
  • next.config.js: Configuration file to customize your Next.js app.

Creating Your First Page

Let’s create a simple Hello World page to get comfortable with the file structure and routing.

Create a file named hello.tsx inside the pages directory:

// pages/hello.tsx

import { NextPage } from 'next';

const Hello: NextPage = () => {
return (
<div>
<h1>Hello, World!</h1>
</div>
);
};

export default Hello;

Now if you run your project using:

npm run dev

You can navigate to http://localhost:3000/hello to see your freshly created "Hello, World!" page.

Adding Static Props

To demonstrate the power of static generation with TypeScript, let’s fetch some static props for our page.

// pages/hello.tsx

import { GetStaticProps, NextPage } from 'next';

type Props = {
message: string;
};

const Hello: NextPage<Props> = ({ message }) => {
return (
<div>
<h1>{message}</h1>
</div>
);
};

export const getStaticProps: GetStaticProps = async () => {
return {
props: {
message: 'Hello with Static Props!',
},
};
};

export default Hello;

Here we’ve utilized getStaticProps, a function specifically used by Next.js to fetch data at build time. This page now pre-renders with a static message.

API Routes

Next.js also makes it easy to create backend logic with API routes. Let’s add a simple API route for understanding.

Create a new file named hello.ts in the pages/api directory:

// pages/api/hello.ts

import { NextApiRequest, NextApiResponse } from 'next';

const handler = (req: NextApiRequest, res: NextApiResponse) => {
res.status(200).json({ message: 'Hello from API!' });
};

export default handler;

By doing this, we’ve defined an API route at http://localhost:3000/api/hello, which returns a simple JSON message.

Wrap-Up

We’ve just scratched the surface of what Next.js can do. From setting up a simple Next.js app with TypeScript to implementing pre-rendering and API routes, there’s a lot more to explore: dynamic routing, middleware, custom document, and much more.

Next.js is a robust tool that can greatly enhance the performance and SEO of your web apps while simplifying server-side programming with its clean, React-based architecture. As you delve deeper, you’ll find even more reasons to make Next.js a staple in your web development toolkit.

So what are you waiting for? Start exploring Next.js today, and elevate your React applications to the next level! Happy coding!

--

--

@rnab
@rnab

Written by @rnab

Typescript, Devops, Kubernetes, AWS, AI/ML, Algo Trading

No responses yet