Introduction to Next.js: Building Modern React Applications

Abu Sayed
3 min readNov 15, 2023

--

Learn the fundamentals of Next.js, a powerful React framework for building modern web applications. Discover core concepts like server-side rendering, static site generation, data fetching, routing, and project structure. Enhance your React development skills with Next.js and create performant, SEO-friendly web apps.

What is Next.js?

Next.js is a React framework that enhances the capabilities of React, specifically designed for building server-rendered and static web applications. It offers a variety of features and conventions that streamline the development process, resulting in faster, more performant, and SEO-friendly applications.

Core Concepts of Next.js

Server-Side Rendering (SSR): Next.js generates HTML content on the server, providing a fully rendered page to the user's browser. This enhances initial page load performance and improves SEO.

  • Static Site Generation (SSG): Next.js pre-renders HTML at build time, resulting in blazing-fast page load times and optimal SEO. This approach is ideal for content that doesn't change frequently.
  • Client-Side Rendering (CSR): Next.js supports client-side rendering, allowing dynamic content updates and interactivity. This approach is suitable for applications that require real-time data updates.
  • File-Based Routing: Next.js utilizes a file-based routing system, where the structure of the pages directory determines the application's URL paths. This simplifies routing configuration and makes it intuitive to manage page hierarchies.

Data Fetching in Next.js

Next.js provides various methods for fetching data, including:

  • getStaticProps: Retrieves data during the build process, enabling static site generation.
  • getServerSideProps: Fetches data on each request, ensuring that the data is always up-to-date.
  • useSWR: A React hook for data fetching that provides caching, revalidation, and error handling.

Routing in Next.js

Next.js offers a powerful routing system that supports various features:

  • Dynamic Routes: Define routes with parameters to handle dynamic content.
  • Nested Routes: Create nested routes to organize complex page hierarchies.
  • Catch-All Routes: Handle unmatched URLs with custom behavior.

Project Structure in Next.js

Next.js projects follow a structured directory layout:

  • pages: Contains React components that represent application pages.
  • public: Stores static assets like images, fonts, and stylesheets.
  • styles: Holds global CSS styles for the application.
  • components: Re-usable React components shared across the application.

Example: Creating a Simple Blog Post

Consider a basic blog post page that displays a title, author, and content:

JavaScript

import React from 'react'; export default function BlogPost({ title, author, content }) { return ( <div> <h1>{title}</h1> <p>By {author}</p> <p>{content}</p> </div> ); } 

To fetch the blog post data, you can use getStaticProps to retrieve it during build time:

JavaScript

import React from 'react'; import { getStaticProps } from 'next'; const BlogPostPage = ({ title, author, content }) => { return <BlogPost title={title} author={author} content={content} />; }; export async function getStaticProps() { const response = await fetch('https://api.example.com/posts/1'); const data = await response.json(); return { props: { title: data.title, author: data.author, content: data.content, }, }; } export default BlogPostPage;

This code fetches the blog post data from an API and passes it to the BlogPost component as props.

Conclusion

Next.js has emerged as a popular React framework for building modern web applications. Its focus on performance, SEO, and developer experience makes it a compelling choice for creating scalable, user-friendly web apps. By leveraging Next.js's features and conventions, developers can streamline their workflow and build applications that deliver exceptional user experiences.

--

--

Abu Sayed
Abu Sayed

Written by Abu Sayed

Bangladeshi Full Stack Web Dev, Sys Admin & DevOps Engineer. Skills: Data analysis, SQL, Unity, C#. Python, PHP & Laravel. Me on: bd.linkedin.com/in/imabusayed

No responses yet