React

The ONLYOFFICE Docs React component integrates ONLYOFFICE Docs into React projects.

Prerequisites

This procedure requires Node.js (and npm).

Creating the demo React application with ONLYOFFICE Docs editor

This procedure creates a basic React application and installs an ONLYOFFICE Docs editor in it.

  1. Create a new React project named onlyoffice-react-demo using the Create React App package:

    npx create-react-app onlyoffice-react-demo
    
  2. Go to the newly created directory:

    cd onlyoffice-react-demo
    
  3. Install ONLYOFFICE Docs React component from npm and save it to the package.json file with --save:

    npm install --save @onlyoffice/document-editor-react
    

    You can also use the following yarn command:

    yarn add @onlyoffice/document-editor-react
    
  4. Open the ./src/App.js file in the onlyoffice-react-demo project and replace its contents with the following code:

    import {DocumentEditor} from "@onlyoffice/document-editor-react"
    import React, {useRef} from "react"
    
    function onDocumentReady(event) {
      console.log("Document is loaded")
    }
    
    function onLoadComponentError(errorCode, errorDescription) {
      switch (errorCode) {
      case -1: // Unknown error loading component
        console.log(errorDescription)
        break
    
      case -2: // Error load DocsAPI from http://documentserver/
        console.log(errorDescription)
        break
    
      case -3: // DocsAPI is not defined
        console.log(errorDescription)
        break
      }
    }
    
    export default function App() {
      return (
        <DocumentEditor
          id="docxEditor"
          documentServerUrl="http://documentserver/"
          config={{
            document: {
              fileType: "docx",
              key: "Khirz6zTPdfd7",
              title: "Example Document Title.docx",
              url: "https://example.com/url-to-example-document.docx",
            },
            documentType: "word",
            editorConfig: {
              callbackUrl: "https://example.com/url-to-callback.ashx",
            },
          }}
          events_onDocumentReady={onDocumentReady}
          onLoadComponentError={onLoadComponentError}
        />
      )
    }
    

    Replace the following lines with your own data:

    • http://documentserver/ - replace with the URL of your server;
    • https://example.com/url-to-example-document.docx - replace with the URL to your file;
    • https://example.com/url-to-callback.ashx - replace with your callback URL (this is required for the saving functionality to work).

    This JavaScript file will create the App component containing the ONLYOFFICE Docs editor configured with basic features.

  5. Test the application using the Node.js development server:

    • To start the development server, navigate to the onlyoffice-react-demo directory and run:

      npm run start
      
    • To stop the development server, select on the command line or command prompt and press Ctrl+C.

Deploying the demo React application

The easiest way to deploy the application to a production environment is to install serve and create a static server:

  1. Install the serve package globally:

    npm install -g serve
    
  2. Serve your static site on the 3000 port:

    serve -s build
    

    Another port can be adjusted using the -l or --listen flags:

    serve -s build -l 4000
    
  3. To serve the project folder, go to it and run the serve command:

    cd onlyoffice-react-demo
    serve
    

Now you can deploy the application to the created server:

  1. Navigate to the onlyoffice-react-demo directory and run:

    npm run build
    

    The build directory will be created with a production build of your app.

  2. Copy the contents of the onlyoffice-react-demo/build directory to the root directory of the web server (to the onlyoffice-react-demo folder).

The application will be deployed on the web server (http://localhost:3000 by default).

ONLYOFFICE Docs React component API

Properties

NameTypeDefaultDescription
id*stringnullComponent unique identifier.
documentServerUrl*stringnullAddress of ONLYOFFICE Docs.
config*objectnullGeneric configuration object for opening a file with token.
onLoadComponentError(errorCode: number, errorDescription: string) => voidnullThe function called when an error occurs while loading a component.
document_fileTypestringnullThe type of the file.
document_titlestringnullThe file name.
documentTypestringnullThe document type.
heightstringnullDefines the document height in the browser window.
typestringnullDefines the platform type used to access the document (desktop, mobile or embedded).
widthstringnullDefines the document width in the browser window.
events_onAppReady(event: object) => voidnullThe function called when the application is loaded into the browser.
events_onDocumentStateChange(event: object) => voidnullThe function called when the document is modified.
events_onMetaChange(event: object) => voidnullThe function called when the meta information of the document is changed via the meta command.
events_onDocumentReady(event: object) => voidnullThe function called when the document is loaded into the document editor.
events_onInfo(event: object) => voidnullThe function called when the application opened the file.
events_onWarning(event: object) => voidnullThe function called when a warning occurs.
events_onError(event: object) => voidnullThe function called when an error or some other specific event occurs.
events_onRequestSharingSettings(event: object) => voidnullThe function called when the user is trying to manage document access rights by clicking Change access rights button.
events_onRequestRename(event: object) => voidnullThe function called when the user is trying to rename the file by clicking the Rename... button.
events_onMakeActionLink(event: object) => voidnullThe function called when the user is trying to get link for opening the document which contains a bookmark, scrolling to the bookmark position.
events_onRequestInsertImage(event: object) => voidnullThe function called when the user is trying to insert an image by clicking the Image from Storage button.
events_onRequestSaveAs(event: object) => voidnullThe function called when the user is trying to save file by clicking Save Copy as... button.
events_onRequestMailMergeRecipients(event: object) => voidnullThe function called when the user is trying to select recipients data by clicking the Mail merge button.
events_onRequestCompareFile(event: object) => voidnullThe function called when the user is trying to select document for comparing by clicking the Document from Storage button.
events_onRequestEditRights(event: object) => voidnullThe function called when the user is trying to switch the document from the viewing into the editing mode by clicking the Edit Document button.
events_onRequestHistory(event: object) => voidnullThe function called when the user is trying to show the document version history by clicking the Version History button.
events_onRequestHistoryClose(event: object) => voidnullThe function called when the user is trying to go back to the document from viewing the document version history by clicking the Close History button.
events_onRequestHistoryData(event: object) => voidnullThe function called when the user is trying to click the specific document version in the document version history.
events_onRequestRestore(event: object) => voidnullThe function called when the user is trying to restore the file version by clicking the Restore button in the version history.

* - required field

Installing Storybook

Install Storybook to develop UI components in isolation:

  1. Change the address of the document server in the config/default.json file:

    {
      "documentServerUrl": "http://documentserver/"
    }
    

    where documentserver is the name of the server with ONLYOFFICE Docs installed.

  2. Build Storybook with the following command:

    yarn build-storybook
    
  3. Start Storybook:

    yarn storybook
    

Developing ONLYOFFICE Docs React component

  1. Clone project from the GitHub repository:

    git clone https://github.com/ONLYOFFICE/document-editor-react
    
  2. Install the project dependencies:

    yarn install
    
  3. Test the component:

    yarn test
    
  4. Build the project:

    yarn rollup
    
  5. Create the package:

    npm pack
    

Feedback and support

In case you have any issues, questions, or suggestions for the ONLYOFFICE Docs React component, please refer to the Issues section.

Get Help

  • If you have any questions about ONLYOFFICE Docs, try the FAQ section first.
  • You can request a feature or report a bug by posting an issue on GitHub.
  • You can also ask our developers on ONLYOFFICE forum (registration required).