{"id":4009,"date":"2023-11-04T23:13:59","date_gmt":"2023-11-04T23:13:59","guid":{"rendered":"http:\/\/localhost:10003\/how-to-use-next-js-for-server-side-rendering-in-react\/"},"modified":"2023-11-05T05:48:25","modified_gmt":"2023-11-05T05:48:25","slug":"how-to-use-next-js-for-server-side-rendering-in-react","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-use-next-js-for-server-side-rendering-in-react\/","title":{"rendered":"How to Use Next.js for Server-Side Rendering in React"},"content":{"rendered":"

In this tutorial, we will learn how to use Next.js for server-side rendering in a React application. Next.js is a popular React framework that provides built-in server-side rendering capabilities, making it easier to create fast and SEO-friendly React applications.<\/p>\n

Server-side rendering (SSR) is the process of rendering the initial page on the server and sending it to the client. This improves the performance and allows search engines to crawl and index the content, enhancing the website’s visibility.<\/p>\n

Before we start, make sure you have Node.js and npm (Node package manager) installed on your machine. You can download them from the official Node.js website.<\/p>\n

Step 1: Creating a Next.js Project<\/h2>\n

To get started, open your terminal and navigate to the directory where you want to create your project. Then, run the following command to initialize a new Next.js project:<\/p>\n

npx create-next-app my-app\n<\/code><\/pre>\n

This command creates a new directory called “my-app” and installs all the necessary dependencies for a Next.js project.<\/p>\n

Next, navigate to the project directory:<\/p>\n

cd my-app\n<\/code><\/pre>\n

Step 2: Creating a Basic Next.js Page<\/h2>\n

Next.js follows the convention of creating pages as components inside the “pages” directory. Let’s create our first Next.js page.<\/p>\n

Inside the “pages” directory, create a new file called “index.js”. Open the file in your favorite text editor and add the following code:<\/p>\n

function HomePage() {\n  return <h1>Hello Next.js!<\/h1>;\n}\n\nexport default HomePage;\n<\/code><\/pre>\n

In this example, we created a simple functional component called “HomePage” that returns an <h1><\/code> element with the text “Hello Next.js!”.<\/p>\n

Step 3: Starting the Next.js Development Server<\/h2>\n

To start the Next.js development server and see our page in action, run the following command in your terminal:<\/p>\n

npm run dev\n<\/code><\/pre>\n

This command starts the development server and opens your application in your default web browser at `http:\/\/localhost:3000`. You should see the “Hello Next.js!” text on the page.<\/p>\n

Step 4: Adding Server-Side Rendering to Our Page<\/h2>\n

Now, let’s modify our page to use server-side rendering instead of client-side rendering.<\/p>\n

Replace the contents of “pages\/index.js” with the following code:<\/p>\n

import fetch from 'isomorphic-unfetch';\n\nfunction HomePage({ data }) {\n  return (\n    <div>\n      <h1>Hello Next.js!<\/h1>\n      <p>{data}<\/p>\n    <\/div>\n  );\n}\n\nexport async function getServerSideProps() {\n  const res = await fetch('https:\/\/api.example.com\/data');\n  const data = await res.text();\n\n  return {\n    props: {\n      data,\n    },\n  };\n}\n\nexport default HomePage;\n<\/code><\/pre>\n

In this code, we made the following changes:<\/p>\n