# SpreadJS with NextJS

## Content

SpreadJS supports NextJS, a react-based framework that lets you develop Web Applications for different platforms such as Windows, Linux, and Mac. NextJS provides all the features you need for production, be it TypeScript support, hybrid static & server rendering, route pre-fetching, or smart bundling.
For more information, refer to [https://nextjs.org/docs/getting-started](https://nextjs.org/docs/getting-started).
In this tutorial, we build a Next.js application that uses SpreadJS.

> **Note**: SpreadJS now supports NextJS version 14. To create a Next.js application, ensure the Node.js version installed should be greater than v18.17.

## Create Next.js App

<span class="ui-provider a b c d e f g h i j k l m n o p q r s t u v w x y z ab ac ae af ag ah ai aj ak" dir="ltr">To create a Next.js application, the easiest option is to use the</span> `create-next-app` command on the CLI tool.

1. Run the following command from the command prompt or terminal to create a Next.js TypeScript project.

    ```auto
    npx create-next-app@latest
    ```
2. Enter the project name, such as *nextjs-with-spreadjs*.
3. When the terminal prompts the configuration messages, select the required option and press the Enter key to confirm.
    Wait a while as the terminal installs and creates the project.
4. Once the dependencies are installed, type the following commands in the terminal to start the project.

    ```auto
    cd nextjs-with-spreadjs
    npm run dev
    ```
5. Open http://localhost:3000 using a browser. Then you will see the welcome page of NextJS.
6. Close the current terminal and open the *nextjs-with-spreadjs* folder using Visual Studio Code or an IDE.

## Install SpreadJS

1. Install the SpreadJS package.

    ```auto
    npm install @mescius/spread-sheets-react
    ```

    Create a folder called *components*, if it does not exist in the applications' root folder.
2. Create a JavaScript XML file called *SpreadSheet.jsx* and add it to the components folder with the following code into it.
    You can enter a valid SpreadJS license key before initializing the component.

    ```javascript
    "use client";
    
    import React, { useState } from "react";
    import { SpreadSheets, Worksheet, Column } from "@mescius/spread-sheets-react";
    import * as GC from "@mescius/spread-sheets";
    
    // Licensing SpreadJS
    var SpreadJSKey = "xxx";          // Enter a valid license key.
    GC.Spread.Sheets.LicenseKey = SpreadJSKey;
    
    export default function SpreadSheet() {
    
        const [spreadBackColor, setSpreadBackColor] = useState('aliceblue');
        const [sheetName, setSheetName] = useState('Empolyees');
        const [hostStyle, setHostStyle] = useState({
            width: '100%',
            height: '700px'
        });
        const dataArr = [
            {
                "jobTitleName": "Developer",
                "preferredFullName": "Romin Irani",
                "region": "CA",
                "phoneNumber": "408-1234567"
            },
            {
                "jobTitleName": "Developer",
                "preferredFullName": "Neil Irani",
                "region": "CA",
                "phoneNumber": "408-1111111"
            },
            {
                "jobTitleName": "Program Directory",
                "preferredFullName": "Tom Hanks",
                "region": "CA",
                "phoneNumber": "408-2222222"
            }
        ];
        const [data, setData] = useState(dataArr);
        const [columnWidth, setColumnWidth] = useState(200);
        return (
            <SpreadSheets backColor={spreadBackColor} hostStyle={hostStyle}>
                <Worksheet name={sheetName} dataSource={data}>
                    <Column dataField='preferredFullName' width={columnWidth}></Column>
                    <Column dataField='jobTitleName' width={columnWidth}></Column>
                    <Column dataField='phoneNumber' width={columnWidth}></Column>
                    <Column dataField='region' width={columnWidth}></Column>
                </Worksheet>
            </SpreadSheets>);
    }
    ```
3. Change the index file content. Replace the default content of the *app/page.tsx* file with the following code.

    ```javascript
    import dynamic from "next/dynamic";
    
    const SpreadSheet = dynamic(
      () => {
        return import("../components/SpreadSheet");
      },
      { ssr: false }
    );
    
    export default function Home() {
      return (
        <div>
          <h1>
          Next.JS + Spreadsheets demo
          </h1>
          <SpreadSheet />
        </div>
      )
    }
    ```

## Import Styles

Import the SpreadJS CSS into the application by adding the following line in the *globals.css* file, which is present in the *app* folder.

```auto
@import '@mescius/spread-sheets/styles/gc.spread.sheets.excel2016colorful';
```

## Run and Test App

You can run the application by using the `npm run dev` commands.
By default, the project runs at http://localhost:3000/.