# Migrating from React to Next.js

### Migration from ReactJS to NextJS

To migrate a ReactJS application to a Next.js application, follow these steps :thumbsup:

#### &#x20;**1. Set up a Next.js project**:&#x20;

◦ Make a directory for the new Next.js project using the terminal. For example, create a folder named <mark style="color:orange;">next-migration-project</mark> on the desktop and navigate into it.

&#x20;◦ Initialize a package.json file in the project directory. This file holds the key properties for running the project, including dependencies. You can create an empty package.json file using the echo command in the terminal. &#x20;

```bash
// package.json
echo "{}" > package.json
```

◦ Alternatively, you can use the terminal in VS Code.&#x20;

#### **2. Install Dependencies**:&#x20;

◦ **Install react, react-dom, and next** as dependencies using npm. Run the command <mark style="color:orange;">**npm install react react-dom next**</mark> in the terminal. This downloads the necessary packages into the project.&#x20;

```bash
// install Dependencies
npm install react react-dom next
```

◦ The <mark style="color:orange;">**package.json**</mark> file will be automatically updated with the dependencies and their versions. A <mark style="color:red;">**package-lock.json**</mark> file is also created to lock in the versions.&#x20;

◦ The **node\_modules** folder contains the actual files of the dependencies.&#x20;

#### **3. Migrate React code**:&#x20;

◦ Move the existing React code (e.g., <mark style="color:orange;">**index.html**</mark>) into the Next.js project directory.&#x20;

◦ Remove the <mark style="color:purple;">React</mark> and <mark style="color:purple;">ReactDOM</mark> tags from the HTML file because Next.js manages these dependencies. <br>

◦ Remove the <mark style="color:purple;">**HTM**</mark>**L** and <mark style="color:purple;">**body tags**</mark>, as Next.js creates these. Also, remove the <mark style="color:purple;">**\<div id="app">**</mark>.\
◦ Remove the <mark style="color:purple;">**\<script type="text/babel">**</mark> <mark style="color:purple;"></mark><mark style="color:purple;">ta</mark>g.

#### &#xD;**4. Convert to JSX:**&#xD; ◦ Rename <mark style="color:orange;">**index.html**</mark> to index.js or <mark style="color:orange;">**index.jsx**</mark>.

#### &#xD;5\. Import React:&#xD; ◦ <mark style="color:purple;">Import React</mark> at the top of the file using import React from 'react' or, to destructure the useState hook, use <mark style="color:purple;">import { useState } from 'react'</mark>.

#### &#xD;**6. Export the component:**&#xD; ◦ Export the main component function using <mark style="color:orange;">**export default function HomePage().**</mark>

#### &#xD;&#x37;**. Set up the development script:**&#xD; ◦ In package.json, add a "**scripts**" section to define a development script.&#xD; ◦ Add the following script: <mark style="color:purple;">**"dev": "next dev"**</mark>.

#### &#xD;**8. Create the pages directory:**&#xD; ◦ Create a folder named pages in the root of the Next.js project. Next.js requires this specific folder structure.&#xD; ◦ Move the <mark style="color:orange;">**index.js**</mark> or <mark style="color:orange;">**index.jsx**</mark> file into the <mark style="color:purple;">**pages**</mark> folder.

#### &#xD;**9. Run the development server:**&#xD; ◦ Run the command <mark style="color:purple;">**npm run dev**</mark> in the terminal to start the Next.js development server.&#xD; ◦ Open a browser and navigate to <mark style="color:green;">**<http://localhost:3000>**</mark> to view the application.&#xD;<br>

After completing these steps, the React application should be running in a Next.js environment. Next.js handles JSX transpilation and provides a streamlined development experience.

{% tabs %}
{% tab title="React-from-scratch" %}

```html
// index.html - Before migration
<html>
  <body>
    <div id="root"></div>
    <script src="https://unpkg.com/react@17/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
    <script type="text/babel">
      function HomePage() {
        const [likes, setLikes] = React.useState(0)

        const names = [
          'Bakytbekov Jakypbai',
          'Boobekov Dastan',
          'Kuralbekov Nurjigit',
          'Kyzdarbekov Nurdavlet',
        ]

        function handleClick() {
          console.log('Increment Like count')
          setLikes(likes + 1)
        }
        return (
          <>
            <div>
              <Header title="I love ReactJS" author="Caleb Park" />
              <Header title="I love NextJS" author="Samara" />
            </div>
            <div>
              <ul>
                {names.map((name, index) => (
                  <li key={index}> {name} </li>
                ))}
              </ul>
            </div>
            <button onClick={handleClick}> Like : {likes} </button>
          </>
        )
      }

      function Header({ title, author }) {
        console.log(title, author)
        return <h1> {author ? title : 'No Book Found by' + author}</h1>
      }

      const app = document.getElementById('root')
      ReactDOM.render(<HomePage />, app)
    </script>
  </body>
</html>
```

{% endtab %}

{% tab title="From-React-to-NextJS" %}

```jsx
// index.jsx
import { useState } from 'react'

function HomePage() {
  const [likes, setLikes] = useState(0)

  const names = [
    'Bakytbekov Jakypbai',
    'Boobekov Dastan',
    'Kuralbekov Nurjigit',
    'Kyzdarbekov Nurdavlet',
  ]

  function handleClick() {
    console.log('Increment Like count')
    setLikes(likes + 1)
  }
  return (
    <>
      <div>
        <Header title="I love ReactJS" author="Caleb Park" />
        <Header title="I love NextJS" author="Samara" />
      </div>
      <div>
        <ul>
          {names.map((name, index) => (
            <li key={index}> {name} </li>
          ))}
        </ul>
      </div>
      <button onClick={handleClick}> Like : {likes} </button>
    </>
  )
}

function Header({ title, author }) {
  console.log(title, author)
  return <h1> {author ? title+' by '+ author : 'No Book Found by' + author}</h1>
}

/*       const app = document.getElementById('root')
      ReactDOM.render(<HomePage />, app) */

export default HomePage
```

{% endtab %}
{% endtabs %}
