Document Viewer Integration in React Application
- 3 minutes to read
The server-side model stores a report and provides it to the HTML5 Document Viewer integrated in the client React JavaScript application.
The project consists of server and client parts.
Server (Back-End) Application
Refer to the following help topic to create a back-end application: Document Viewer Server-Side Application (ASP.NET Core)
Client (Front-End) Application
Create a folder (JS in our example) to store application files. Launch the command prompt to perform the following steps to create and configure the client part.
Create a new React application in the react-document-viewer folder:
npx create-react-app react-document-viewer
Switch to the root application folder:
cd react-document-viewer
Install DevExpress packages:
npm install devextreme devexpress-reporting @devexpress/analytics-core
Open the App.js file in the src folder (JS\react-document-viewer\src) and substitute its content with the following code snippet:
import './App.css'; import { useEffect, useRef } from 'react'; import { DxReportViewer } from 'devexpress-reporting/dx-webdocumentviewer'; import * as ko from 'knockout'; const ReportViewer = () => { const reportUrl = ko.observable("TestReport"); const viewerRef = useRef(); const requestOptions = { host: "https://localhost:54114/", invokeAction: "DXXRDV" }; useEffect(() => { const viewer = new DxReportViewer(viewerRef.current, { reportUrl, requestOptions }); viewer.render(); return () => viewer.dispose(); }) return (<div ref={viewerRef}></div>); } function App() { return (<div style={{ width: "100%", height: "1000px" }}> <ReportViewer /> </div>); } export default App;
This code snippet declares the ReportViewer component and returns it with the App function.
Important
You can modify App.js content to specify the correct server-side port (54114 in this example) and report name (TestReport in this example).
Open the App.css file in the src folder (JS\react-document-viewer\src) and append the following style sheet references:
/* ... */ @import url("../node_modules/devextreme/dist/css/dx.light.css"); @import url("../node_modules/@devexpress/analytics-core/dist/css/dx-analytics.common.css"); @import url("../node_modules/@devexpress/analytics-core/dist/css/dx-analytics.light.css"); @import url("../node_modules/devexpress-reporting/dist/css/dx-webdocumentviewer.css");
Run the server (back-end) application.
Navigate to the client application’s root folder (JS\react-document-viewer), open the command prompt, and run the client application:
npm start
The client application opens the browser at http://localhost:3000/. It displays the Document Viewer with the TestReport report.
Troubleshooting
When you start the application, you may encounter the following problems:
Page is blank
The Document Viewer page is blank. The following error message is displayed at the bottom of the page: The page is blank because the Document Viewer failed to load the report. Consult the developer for assistance. Use development mode for detailed information. If the page does not display the Document Viewer, press F12 to invoke DevTools and inspect the console messages.
Check the following:
- The backend application is up and running.
- The backend application runs on the port specified in the host setting of the Document Viewer component.
- The application’s URI is compliant with the CORS policy specified in your backend application.
- The reportName value matches an existing report. For the backend application it means that the Reports folder contains the reportName.repx file or the ReportsFactory.Reports dictionary contains the reportName entry (if the backend application originated from the DevExpress template).
- The version of the DevExpress scripts (npm packages) should match the version of the server-side libraries (NuGet packages). You can enable Development Mode to check for library version mismatch on every request to the server. For more information, take a look at the following help topic: Server-Side Libraries Version.
Review the following help topic for more information: Troubleshooting.