Creating a Next.js Application Using Syncfusion React Components

20 Jan 20254 minutes to read

This section provides a step-by-step guide for setting up a Next.js application and integrating the Syncfusion React File Manager component.

What is Next.js?

Next.js is a React framework that makes it easy to build fast, SEO-friendly, and user-friendly web applications. It provides features such as server-side rendering, automatic code splitting, routing, and API routes, making it an excellent choice for building modern web applications.

Prerequisites

Before getting started with the Next.js application, ensure the following prerequisites are met:

  • Node.js 18.17 or later.

  • The application is compatible with macOS, Windows, and Linux operating systems.

Create a Next.js application

To create a new Next.js application, use one of the commands that are specific to either NPM or Yarn.

npx create-next-app@latest
yarn create next-app

Using one of the above commands will lead you to set up additional configurations for the project as below:

1.Define the project name: Users can specify the name of the project directly. Let’s specify the name of the project as ej2-nextjs-file-manager.

√ What is your project named? » ej2-nextjs-file-manager

2.Select the required packages.

√ What is your project named? ... ej2-nextjs-file-manager
√ Would you like to use TypeScript? ... No / `Yes`
√ Would you like to use ESLint? ... No / `Yes`
√ Would you like to use Tailwind CSS? ... `No` / Yes
√ Would you like to use `src/` directory? ... No / `Yes`
√ Would you like to use App Router? (recommended) ... No / `Yes`
√ Would you like to customize the default import alias? ... `No`/ Yes
Creating a new Next.js app in D:\ej2-nextjs-file-manager.

3.Once complete the above mentioned steps to create ej2-nextjs-file-manager, navigate to the directory using the below command:

cd ej2-nextjs-file-manager

The application is ready to run with default settings. Now, let’s add Syncfusion components to the project.

Install Syncfusion React packages

Syncfusion React component packages are available at npmjs.com. To use Syncfusion React components in the project, install the corresponding npm package.

Here, the React File Manager component is used as an example. To install the React File Manager component in the project, use the following command:

npm install @syncfusion/ej2-react-filemanager --save
yarn add @syncfusion/ej2-react-filemanager

Import Syncfusion CSS styles

Syncfusion React components come with built-in themes, which are available in the installed packages. It’s easy to adapt the Syncfusion React components to match the style of your application by referring to one of the built-in themes.

Import the Material theme into the src/app/globals.css file and removed the existing styles in that file, as shown below:

@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-icons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/material.css';
@import '../node_modules/@syncfusion/ej2-layouts/styles/material.css';
@import '../node_modules/@syncfusion/ej2-filemanager/styles/material.css';
@import "../node_modules/@syncfusion/ej2-react-filemanager/styles/material.css";

To know more about built-in themes and CSS reference for individual components, refer to the themes section.

Add Syncfusion React component

Follow the below steps to add the React File Manager component to the Next.js project:

1.Define the File Manager component in the src/app/page.tsx file, as shown below:

'use client'
import { DetailsView, FileManagerComponent, NavigationPane, Toolbar, Inject } from '@syncfusion/ej2-react-filemanager';

function App() {
  let hostUrl: string = "https://ej2-aspcore-service.azurewebsites.net/";
  let ajaxSettings: object = {
          url: hostUrl + "api/FileManager/FileOperations",
          getImageUrl: hostUrl + "api/FileManager/GetImage"
        };

  return (
    <div className="control-section">
        <FileManagerComponent id="file" view="LargeIcons" ajaxSettings = {ajaxSettings} >
          <Inject services={[ NavigationPane, DetailsView, Toolbar]} />
        </FileManagerComponent>
    </div>
  );
}
export default App;

Run the application

To run the application, use the following command:

npm run dev
yarn run dev

To learn more about the functionality of the File Manager component, refer to the documentation.

View the NEXT.js File Manager sample in the GitHub repository.