Batteries-Included: React Application with Next.js

Get Started and Power Up your React Application with Next.js.

Bee Punnotayan
Slalom Build
5 min readJun 27, 2023

--

What is Next.js?

Next.js is a React-based framework that provides infrastructure and simple development experience for server-side rendered (SSR) and static page applications.

In Next.js each page of your application is represented by a React component. The page-based routing system maps each URL path to a corresponding React component. For example, if you have a page located at /about, you would create a React component called About and export it as the default component in a file called about.js in the pages directory.

The page-based routing allows for simplified and intuitive routing based on the file system structure of the project, reducing the need for complex configuration and setup.

Next.js also supports dynamic routes. Dynamic routes allow you to define routes with parameters that will generate pages dynamically at runtime. For example, if you have a page that shows details for a particular product, you could define a dynamic route for /products/:id, where :id is a parameter that can take on any value. You would create a React component called ProductDetail and export it as the default component in a file called [id].js inside a directory called “product” in the pages directory.

Why you should choose Next.js

Next.js has gained popularity among developers due to its ease of use, flexibility, and numerous features that make building complex React applications easier.

Here are some reasons why Next.js is a great choice for React developers:

  1. Server-side rendering: Next.js provides support for server-side rendering out of the box. This means that the server generates the HTML for the initial page load, resulting in faster load times and better search engine optimization (SEO).
  2. Built-in routing: Next.js comes with built-in routing, making it easy to create and manage routes for your application.
  3. API routes: Next.js also provides support for API routes, allowing you to easily create serverless APIs that can be called from the client-side code.
  4. Static site generation: Next.js supports static site generation, which generates static HTML pages at build time.
  5. Extensibility: Next.js is highly extensible with a plugin system that allows you to add new functionality to your application easily.
  6. Combination of client-side and server-side code: Next.js allows developers to build a NodeJS-based API alongside their React apps in the same codebase. Any code saved pages/api is mapped to /api/* route and Next.js treats it like an API endpoint. For example, you can create a pages/api/products.js that returns a list of products. If you visit the https://app-url/api/products URL, you will see the list of products.
  7. Image and font optimization: Next.js introduces a font system called next/font to optimize your fonts and remove external network requests for improved privacy and performance. Additionally, the next/image package can improve the load time of the site or application and maximize image quality.

Why you should not choose Next.js

  1. Runtime performance: While performance of a Next.js application is faster than a React app, it will have slower runtime performance than other frameworks like Svelte.
  2. Large bundle size: A Next.js application bundle can be large, which may negatively impact performance. However, you can use dynamic imports to load a component only when it’s needed (aka lazy loading). Dynamic imports will split your code into smaller chunks which will decrease bundle size.
  3. Lack of built-in state management: In React, the useState hook is used for local state management. The Context API in Next.js does not replace state management libraries. The Context APO can be configured to function as a state management tool, but since that isn’t its intended use, you’ll need to put in more effort to make it work. However, Next.js can be easily integrated with popular libraries like Redux or MobX to manage states globally and efficiently.

Types of projects that are well-suited for Next.js

Now that you are familiar with the basics of Next.js, you should consider if it is a good fit for your project.

  • Complex or large-scale requirements: With its automatic SSR and code-splitting, Next.js simplifies the process of constructing and scaling large applications as it allows you to generate pages statically. You can also use server-side rendering to render your website faster. It may be overkill for small projects that don’t require these advanced features. Using Next.js in these cases can lead to unnecessary complexity and development overhead.
  • Projects that require integration with the React ecosystem: Next.js is built on top of React which means it seamlessly integrates with the React ecosystem. It provides a familiar API and supports all of the features of React.
  • Projects that require rapid deployment and hosting: Next.js offers a wide range of hosting from classic server-based to static site as well as cloud platforms like Vercel, AWS, and Google Cloud. This makes it a perfect fit for projects that require rapid deployment and hosting.

If you decide Next.js is right for you, here’s how to get started.

How to create a Next.js app

Start any new Next project with this command:

npx create-next-app my-project

create-next-app is a package like create-react-app. It gives you a Next.js project with all its dependencies installed (which are next, react, and react-dom) plus some dummy pages and styles.

Your package.json will look like this:

"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
}
  • dev — runs a development server on localhost:3000
  • build — creates a built application ready for deployment
  • start — starts a Next.js production server (you must run next build first)
  • lint — will “lint” your Next.js project using the dev dependency ESLint

Server-side rendering (SSR)

One of the biggest benefits of Next.js is server-side rendering. Here’s a quick overview of how to implement it in your application.

Server-side rendering renders your application’s pages on the server rather than in the client’s browser. This can improve initial page load times and improve search engine optimization for your application.

To enable SSR in Next.js, you can use the getServerSideProps function which is used to fetch data and render it on the server. This function runs on the server, and its return value is passed as props to the component before rendering.

Here’s an example of how to use SSR with Next.js.

export async function getServerSideProps() {
const response = await fetch('https://api.com/data')
const data = await response.json()

return {
props: {
data
}
}
}

export default function Home({ data }) {
return (
<div>
<h1>Welcome to my home page!</h1>
<p>{data}</p>
</div>
)
}

In the above example, the getServerSideProps function fetches data from an API and returns it as props to the component before rendering. The component receives the data prop and displays it in the HTML content. When a user requests this page, the server generates the HTML content and sends it to the client, including the data fetched by getServerSideProps.

Conclusion

As we have discussed in this article, Next.js is an excellent choice for developing fast and seamless websites. It provides the tools and features you need to deliver a seamless user experience from page performance improvements provided by server-side rendering, image and font optimization, code splitting and full-stack development from back-to front.

--

--