Getting started with React DOCX Editor in Next.js

27 Aug 20264 minutes to read

This section provides a step-by-step guide for setting up a Next.js application and integrating the React DOCX Editor (Document Editor) component.

Prerequisites

Create a Next.js application

Step 1: To create a new Next.js application, use one of the commands specific to NPM or Yarn.

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

Using one of the above commands will prompt you to configure additional settings for the project:

Step 2: Specify the name of the project directly.

√ What is your project named? » ej2-nextjs-documenteditor

Step 3: Choose the following configuration options below.

? Would you like to use the recommended Next.js defaults?
>   Yes, use recommended defaults - TypeScript, ESLint, Tailwind CSS, App Router
    No, reuse previous settings
    No, customize settings

Step 4: Once the above steps are complete, navigate to the project directory using the following command:

cd ej2-nextjs-documenteditor

The application is ready to run with default settings. The next steps will add the Syncfusion DOCX Editor component to the project.

Install DOCX Editor packages

The DOCX Editor package is available at npmjs.com.

To install the DOCX Editor component, use the following command:

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

Register a Syncfusion License Key

Before initializing the React DOCX Editor control in Next.js, generate a Syncfusion license key and register it in your application.

Import the required CSS styles

Themes for DOCX Editor can be applied using CSS or SASS files from the npm theme packages, CDN, CRG, or Theme Studio. For more information, see the themes documentation.

This guide uses the Tailwind 3 theme as an example, sourced from the theme package. In this package, each component includes an index.css file that automatically loads all the required dependency styles. To install the Tailwind 3 theme package, use the following command:

npm install @syncfusion/ej2-tailwind3-theme
yarn add @syncfusion/ej2-tailwind3-theme

By default, Vite projects include a app/global.css file with default styles. These default styles may conflict with Syncfusion component styles. Clear all content from the app/global.css file to prevent style conflicts.

The required styles for the DOCX Editor are imported in the app/global.css file:

@import "../node_modules/@syncfusion/ej2-tailwind3-theme/styles/document-editor/index.css";
@import "../node_modules/@syncfusion/ej2-tailwind3-theme/styles/document-editor-container/index.css";

Initialize the DOCX Editor

Add the DOCX Editor component to your application. In the app/page.tsx file, add the following code to initialize the component:

'use client'
import { DocumentEditorContainerComponent, Toolbar } from '@syncfusion/ej2-react-documenteditor';

DocumentEditorContainerComponent.Inject(Toolbar);

export default function Home() {
  return (
      <DocumentEditorContainerComponent 
        id="container" 
        height="590px"
        // Use the following service URL only for demo purposes
        serviceUrl="https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/" 
        enableToolbar={true}>
      </DocumentEditorContainerComponent>
  )
}

NOTE

The hosted Web API URL is for demo and evaluation purposes only. For production, host your own web service using the GitHub Web Service example or the Docker image.

Run the application

Run the application using the following command:

npm run dev
yarn run dev

After the application starts, open the localhost URL shown in the terminal. The DOCX Editor is rendered in the browser with a toolbar and an editable document area, as shown below.

Output of React DOCX Editor in Next.js

NOTE

View Sample in GitHub.

Server-side dependencies

The DOCX Editor component requires server-side interactions for the following operations:

  • Opening file formats other than SFDT
  • Pasting with formatting
  • Restrict editing
  • Spell checking
  • Saving as file formats other than SFDT and DOCX

NOTE

If you don’t require the above functionalities, you can deploy the component as a pure client-side solution without any server-side interactions.

For detailed information about server-side dependencies, refer to the Web Services Overview page.

See Also