React Js Vs Next js, Why is Nextjs the best?

Sonjoy Chandra Barman
10 min readJan 11, 2023
React Js Vs Next js, Why is Nextjs the best?

React.js is a JavaScript library for building user interfaces. It allows developers to build reusable UI components and manage the state of a web page or application. React is often used in combination with other libraries or frameworks to build complex web applications.

Next.js is a framework for building web applications using React. It provides a set of features and tools for building server-rendered React applications, such as automatic code splitting, static site generation, and server-side rendering. Next.js also includes a built-in development server and can be used with a variety of popular build tools and frameworks.

In summary, React.js is a JavaScript library for building user interfaces, while Next.js is a framework that builds on React to provide a set of features and tools for building server-rendered applications.

Here are a few reasons why Next.js is often considered to be a good choice for building web applications:

1.Server-side rendering: Next.js allows you to build server-rendered React applications, which can improve the performance and SEO of your app.

Here’s an example of a simple server-rendered React component using Next.js:

// pages/index.js
import React from 'react'

export default function Home() {
return (
<div>
<h1>Welcome to my website!</h1>
<p>This page is server-rendered using Next.js.</p>
</div>
)
}

In this example, pages/index.js is a special file in a Next.js app that maps to the / route. The component inside the file will be rendered on the server and the resulting HTML will be sent to the browser.

In this case, the component just returns some JSX (a syntax extension for JavaScript that allows you to describe the structure of your UI using a declarative syntax) which will be rendered as an HTML on the browser.

You could also use a getInitialProps function to fetch data from an API on the server before rendering the component:

import axios from 'axios';

export default function Home({ data }) {
return (
<div>
<h1>Welcome to my website!</h1>
<p>{data}</p>
</div>
)
}
Home.getInitialProps = async () => {
const { data } = await axios.get('https://jsonplaceholder.typicode.com/todos/1');
return { data };
}

In this example, getInitialProps is a Next.js specific lifecycle method that allows you to fetch data on the server before rendering the component. The function makes a request to a JSON Placeholder API and returns an object containing the data which is then passed to the component as props.

Keep in mind, this is just a basic example, Next.js allows you to do much more than this, it’s a powerful framework with lots of capabilities.

2. Automatic code splitting: Next.js automatically splits your code into smaller chunks, so that users only have to download the JavaScript they need to render the current page. This helps to keep the initial load time of your app low.

In Next.js, code splitting is done automatically, you don’t have to write any code to make it happen. When you build your app, Next.js will analyze your code and create separate chunks for each page and component. This way, when a user visits your app, they will only have to download the JavaScript needed to render the current page, instead of downloading the entire codebase.

Here’s an example of a simple Next.js application with multiple pages:

// pages/index.js
import React from 'react'

export default function Home() {
return <h1>Welcome to my website!</h1>
}
// pages/about.js
import React from 'react'

export default function About() {
return <h1>About me</h1>
}

In this example, we have two pages, the index.js and about.js. When you build your application Next.js will create separate chunks for each page index and about chunks, so that when the user visits the index page, the browser will only download the JavaScript related to the index page and the same thing happens when visiting the about page.

You could test this behavior by analyzing the page’s network tab and see the specific chunk loaded.

This automatic code splitting provided by Next.js helps to improve the performance of your app by reducing the amount of JavaScript that needs to be downloaded on initial load and as a result reducing the load time of your app, which leads to a better user experience.

3. Simple development workflow: Next.js includes a built-in development server, which makes it easy to get started building your app. It also supports hot code reloading, so you don’t have to manually refresh the page after making changes.

Here is an example of a simple development workflow using Next.js:

  1. First, you’ll need to have Node.js and npm (or yarn) installed on your computer.
  2. Next, create a new project directory and navigate into it:
mkdir my-app
cd my-app

3. Use npm (or yarn) to initialize a new Next.js project:

npm init next-app

or

yarn create next-app

4. Once the project is created, navigate into the project directory and start the development server:

cd my-app
npm run dev

or

cd my-app
yarn dev

5. The development server will start on http://localhost:3000 by default. You can now start building your app, and the changes you make will be automatically reflected on the browser without manual refresh

6. When you are ready to deploy your app in production you can use

npm run build

followed by

npm run start

or

yarn build

followed by

yarn start

This will build the production-ready version of your app and starts a Node.js server to serve the app.

4. Static site generation: Next.js can be used to generate a static version of your app, which can be hosted on a CDN and can be easily crawled by search engines.

Next.js supports static site generation, which can be useful for generating a static version of your app that can be easily hosted on a CDN or other static hosting service, and can be easily crawled by search engines. Here’s an example of how you might use Next.js to generate a static version of your app:

  1. First, you’ll need to have Node.js and npm (or yarn) installed on your computer.
  2. Next, create a new project directory and navigate into it:
mkdir my-app
cd my-app

3. Use npm (or yarn) to initialize a new Next.js project:

npm init next-app

or

yarn create next-app

4. Once the project is created, navigate into the project directory and build the static version of your app:

cd my-app
npm run export

or

cd my-app
yarn export

5. Next.js will generate a static version of your app in the out directory. You can now use a static hosting service or a CDN, to host this directory.

6. Also, you can configure the export path of the static version to change the out directory location.

next export --outdir path/to/your/static/directory

That’s it! Your app is now ready to be deployed as a static site.

It’s worth noting that not all features of Next.js are supported when generating a static site, like Server-side rendering, Dynamic imports and some other features. so you need to make sure that your app can work correctly with static generation.

5. Community support: Next.js has a large and active community, which means there are many resources available to help you learn and use the framework. It also has good support for typescript and other popular tools

It’s true that Next.js has a large and active community, which provides many resources to help developers learn and use the framework. Additionally, Next.js also has good support for TypeScript, which is a typed superset of JavaScript, and other popular tools. Here is an example of how you might use TypeScript with Next.js:

  1. First, you’ll need to have Node.js and npm (or yarn) installed on your computer.
  2. Next, create a new project directory and navigate into it:
mkdir my-app
cd my-app

3. Use npm (or yarn) to initialize a new Next.js project:

npm init next-app my-app --template with-typescript

or

yarn create next-app my-app --template with-typescript

4. Once the project is created, navigate into the project directory and start the development server:

cd my-app
yarn dev

5. The development server will start on http://localhost:3000 by default, and your project will be using TypeScript and all the benefits it provides.

6. You can also configure typescript by editing the tsconfig.json file in your project root.

7. You can also use types of many popular libraries like react, redux, graphql and more. Just install the package and the types will automatically be included

That’s it! You can now start building your Next.js app with TypeScript and have the advantage of using a typed language with this framework. In addition, there are a lot of resources and tutorials available online to help you learn and use Next.js with TypeScript, and you can also find many helpful examples and solutions in the community.

6. File-based routing: Next.js uses file-based routing, which means that the file structure of your application also determines the routing. This can make it easier to reason about your application’s structure and organization.

Next.js uses file-based routing, which means that the file structure of your application determines the routing. This can make it easier to reason about your application’s structure and organization. Here’s an example of how file-based routing works in Next.js:

  1. First, you’ll need to have Node.js and npm (or yarn) installed on your computer.
  2. Next, create a new project directory and navigate into it:
mkdir my-app
cd my-app

3. Use npm (or yarn) to initialize a new Next.js project:

npm init next-app

or

yarn create next-app

4. Once the project is created, navigate into the pages directory

cd my-app/pages

5. Inside the pages directory, you will see some examples pages that Next.js creates for you by default.

6. Each file and directory inside the pages directory will automatically become a route. for example if you have a file named about.js this file will automatically be a route for /about

7. You can create nested folders to create nested routes, for example, if you create a folder users inside pages and put a file named [id].js inside it. this file will automatically be a route for /users/:id and you can access the id parameter via the query object in the component.

8. You can also use dynamic imports inside the page component and the file name that comes in the url will be passed to the imported component

Here’s an example of a simple component in the about.js file:

import React from 'react'

const About = () => (
<div>
<h1>About Page</h1>
</div>
)
export default About

That’s it! With file-based routing, Next.js automatically maps the file structure of your application to the routing, making it easy to reason about the structure and organization of your app.

7. Built-in handling of Webpack & Babel config: Next.js comes with a built-in Webpack and Babel configuration, which means that you don’t have to spend time configuring these tools yourself.

Yes, Next.js comes with a built-in Webpack and Babel configuration, which means that you don’t have to spend time configuring these tools yourself. This allows you to focus on building your application without worrying about the underlying build tools. Here’s an example of how you might use Next.js without having to configure Webpack and Babel yourself:

  1. First, you’ll need to have Node.js and npm (or yarn) installed on your computer.
  2. Next, create a new project directory and navigate into it:
mkdir my-app
cd my-app

3. Use npm (or yarn) to initialize a new Next.js project

yarn create next-app

4. Once the project is created, navigate into the project directory and start the development server:

cd my-app
yarn dev

5. The development server will start on http://localhost:3000 by default. You can now start building your app, and Next.js will handle the Webpack and Babel configuration for you.

6. You can also configure your build setting in the next.config.js file. you can add plugins, customize webpack configuration and more.

7. When you are ready to deploy your app in production, you can use

yarn build

followed by

yarn start

This will build the production-ready version of your app and starts a Node.js server to serve the app.

That’s it! With Next.js you don’t have to spend time configuring Webpack and Babel, allowing you to focus on building your application.

It’s worth noting that webpack and Babel are complex tools with a lot of options and configurations, Next.js provides a way to make it simple for developers to get started and handle basic use cases, but if you want to go beyond the default configurations and fine-tune your build process, you can do so by adding a next.config.js file in your project root.

8. Good Performance: Next.js is optimized for performance and built to handle high traffic.

Next.js is optimized for performance and is built to handle high traffic, it has several features to improve the performance of your application.

Here’s an example of how you might use some of the performance-related features of Next.js:

  1. Server-side rendering (SSR): Next.js automatically renders your pages on the server, which can improve the time-to-first-byte (TTFB) and overall load time of your pages.
  2. Automatic code splitting: Next.js automatically splits your JavaScript code into small chunks that are loaded on-demand, which can reduce the initial load time of your pages and improve the overall performance.
  3. Static site generation: As I mentioned earlier, Next.js can also generate a static version of your app, which can be easily hosted on a CDN, and can improve load time and performance.
  4. Built-in support for caching, compression and other performance-related features.
  5. Built-in support for environment-based configuration, for example, in your development environment you can use webpack’s development configuration, and in production environment use webpack’s production configuration.
  6. Built-in support for code-splitting and Dynamic imports, this can help to improve the performance of your application by loading only the necessary code when it’s needed.

Here is an example of how you might use code splitting in your Next.js application.

import dynamic from 'next/dynamic'

const DynamicComponent = dynamic(() => import('../components/SomeComponent'))
const MyPage = () => <DynamicComponent />

In the example above, the component SomeComponent will only be loaded when the MyPage component is rendered, this can help to improve the load time and the performance of your app.

By using these features, Next.js can help you to create high-performance web applications, this does not guarantee a high-performance in all cases, since performance also depends on the specific implementation of the application, but Next.js provide the tools to help you achieve good performance.

It’s worth noting that Next.js is not always the best choice for every situation and that it’s important to evaluate the specific needs of your project when choosing a framework or library.

--

--

Sonjoy Chandra Barman

I am excited to continue my career as a full-stack developer and am always looking for new challenges and opportunities to grow and learn.