Skip to main content
A newer version of this page is available. .

Document Viewer Integration in ReactJS

  • 3 minutes to read

You can use the HTML5 Document Viewer in JavaScript with React by creating two projects: a server (backend) project in ASP.NET MVC implementation, and a client (frontend) part that includes all the necessary styles, scripts, and HTML-templates.

Note

See the Creating a Server Side for the Web Document Viewer topic for details on how to create a backend application.

This document describes how to configure and host the client part:

  1. Create a new folder to store all the files related to the client-side functionality (for instance, name it ClientSide).
  2. Download all the necessary client resources using Bower. Add the bower.json file in the created folder and add the following code to this file:

    {
      "name": "html5-document-viewer-example-react",
      "dependencies": {
        "xtrareportsjs": "~17.2.3",
        "react": "latest",
        "babel": "latest"
      }
    }
    

    Ensure that you correctly specify the required reporting controls’ version.

  3. Open the console, navigate to your application folder and run the following command:

    bower install

    After installation is completed, you can find all the libraries in the bower_components folder.

  4. Create a View file in the root folder (the index.html file in this example) and add links to Bower resources to the head section.

    <head>
        <title></title>
        <link rel="stylesheet" href="bower_components/devextreme/css/dx.common.css" />
        <link rel="stylesheet" href="bower_components/devextreme/css/dx.light.css" />
        <script type="text/javascript" src="bower_components/jquery/dist/jquery.js"></script>
        <script type="text/javascript" src="bower_components/knockout/dist/knockout.js"></script>
        <script type="text/javascript" src="bower_components/devextreme/js/dx.all.js"></script>
        <script type="text/javascript" src="bower_components/jquery-ui/jquery-ui.js"></script>
        <link href="bower_components/jquery-ui/themes/base/jquery-ui.min.css" rel="stylesheet" />
    
        <link href="bower_components/xtrareportsjs/css/web-document-viewer-light.css" rel="stylesheet" />
        <script type="text/html" src="bower_components/xtrareportsjs/html/web-document-viewer.html"></script>
        <script type="text/javascript" src="bower_components/xtrareportsjs/js/dx-designer.js"></script>
        <script type="text/javascript" src="bower_components/xtrareportsjs/js/web-document-viewer.js"></script>
        <script type="text/javascript" src="bower_components/xtrareportsjs/js/localization/web-document-viewer.en.js"></script>
    
        <script type="text/javascript" src="bower_components/react/react.development.js"></script>
        <script type="text/javascript" src="bower_components/react/react-dom.development.js"></script>
        <script type="text/javascript" src="bower_components/babel/browser.js"></script>
    </head>
    

    Make sure that you meet all the requirements for deploying the control on the client. Refer to Document Viewer Requirements and Limitations for a complete client resource list.

    Note that this sample uses a script with a predefined localization dictionary.

  5. Add a JSX file (example.jsx) to provide the required data to the View.

    Create the ReportViewer and Root classes that extend the base React.Component class as shown below. Use the dxReportViewer binding to render the Document Viewer element.

    "use strict"
    const host = 'http://localhost:54114/';
    
    class ReportViewer extends React.Component {
        constructor(props) {
            super(props);
            this.reportUrl = ko.observable(props.reportUrl);
            this.requestOptions = {
                host,
                invokeAction: "WebDocumentViewer/Invoke"
            };
        }
        render() {
            return (<div ref="viewer" data-bind="dxReportViewer: $data"></div>);
        }
        componentWillReceiveProps(newProps) {
            this.reportUrl(newProps.reportUrl);
        }
        componentDidMount() {
            ko.applyBindings({
                reportUrl: this.reportUrl,
                requestOptions: this.requestOptions
            }, this.refs.viewer);
        }
        componentWillUnmount() {
            ko.cleanNode(this.refs.viewer);
        }
    }
    
    class Root extends React.Component {
        constructor(props) {
            super(props);
            this.state = { value: "Products", reports: [] };
        }
    
        render() {
            return (<div style = {{width: "100%", height: "1000px"}}>
                <ReportViewer reportUrl={this.state.value} />
            </div>);
        }
    }
    
    ReactDOM.render(<Root />, document.getElementById("viewerhost"))
    
  6. Link the file created at the previous step in the index.html file:

    ...
    
    <body>
        <div id="viewerhost"></div>
        <script type="text/babel" src="example.jsx"></script>
    </body>
    
  7. Host your client-side part on the web server.

    For instance, start the Internet Information Services (IIS) Manager, right-click the Sites item in the Connections section and select Add Website. In the invoked dialog, specify the site name, the path to the folder with the client-side functionality and the website’s IP address.

    host-react-example-in-iis

  8. For this example to work correctly, you should first run a backend project in Visual Studio before running the client part.
See Also