Create a React Front-End Application with a Document Viewer (Next.js)
- 5 minutes to read
Important
Familiarity with React basic concepts and patterns is essential prior to the use of this documentation. If you require a review, please refer to React documentation for a getting-started tutorial
The Web Document Viewer is used in applications that contain client and server parts:
- Client
- A Web Document Viewer integrated in a client React application displays a report provided by the server-side model.
- Server
- The server is an ASP.NET Core application that handles client data requests and provides access to data sources, report storage, and other back-end capabilities.
This tutorial creates and configures a client React application and a server ASP.NET Core backend. The client is created with the help of Next.js and contains the Web Document Viewer control.
Tip
You can also use our DevExpress project templates to create a React Reporting application:
#Prerequisites
- Node.js 18.17 or later
- .NET 8 SDK or later
- Visual Studio 2022 (v17.0) or higher
Note the following details about package versions:
- The script version on the client should match the library version on the server.
- DevExpress npm package versions should be identical.
#Create a Server Application (Back-End)
#Use the DevExpress CLI Template
You can use DevExpress CLI Templates to create an ASP.NET Core back-end application. Begin with the steps below:
Install DevExpress ASP.NET Core project templates from nuget.org:
dotnet new install DevExpress.AspNetCore.ProjectTemplates
Create a back-end Reporting application for a Document Viewer:
consoledotnet new dx.aspnetcore.reporting.backend -n ServerApp --add-designer false
You can use the following parameters to see available command options:
-? | -h | --help
.Enable cross-origin requests (CORS). Specify the policy that allows any local application to access the report’s back-end. Use the SetIsOriginAllowed method to set it up.
Call the UseCors method and pass the policy name as a parameter. The
UseCors
method should be called after theUseRouting
method and before any MVC-related code. Place theUseCors
method before theUseMvc
orUseEndpoints
methods.Open the application startup file and insert the following code:
var builder = WebApplication.CreateBuilder(args); builder.Services.AddCors(options => { options.AddPolicy("AllowCorsPolicy", builder => { // Allow all ports on local host. builder.SetIsOriginAllowed(origin => new Uri(origin).Host == "localhost"); builder.AllowAnyHeader(); builder.AllowAnyMethod(); }); }); var app = builder.Build(); app.UseRouting(); app.UseCors("AllowCorsPolicy"); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); app.Run();
To run the server-side application, run the following command:
consolecd ServerApp dotnet run
#Use Visual Studio Template
To create a back-end application from a Microsoft or DevExpress Template in Visual Studio, review the following help topics:
- Document Viewer Server-Side Application (ASP.NET Core)
- Document Viewer’s Server-Side Configuration (ASP.NET MVC)
#Create a Client Application (Front-End)
In the command prompt, create a React application with Next.js:
cmdnpx create-next-app@latest react-document-viewer
Navigate to the project folder:
cmdcd react-document-viewer
Install the devexpress-reporting-react npm package:
cmdnpm install devexpress-reporting-react@24.2-stable
Open the app/page.tsx file and substitute its contents with the following code excerpt:
TypeScript'use client'; import 'devextreme/dist/css/dx.light.css'; import '@devexpress/analytics-core/dist/css/dx-analytics.common.css'; import '@devexpress/analytics-core/dist/css/dx-analytics.light.css'; import 'devexpress-reporting/dist/css/dx-webdocumentviewer.css'; import ReportViewer, { RequestOptions } from 'devexpress-reporting-react/dx-report-viewer'; function App() { return ( <ReportViewer reportUrl="TestReport"> <RequestOptions host="http://localhost:5000/" invokeAction="DXXRDV" /> </ReportViewer> ) } export default App
This code snippet declares the
ReportViewer
component and returns it with theApp
function.Specify the correct server-side port (the
host
variable) and report name (thereportUrl
variable).
#Run the Project
Run the server application.
Make sure to specify the correct server-side port (5000 in this example) and report name (TestReport in this example) in the app/page.tsx file.
Run the client application:
cmdnpm run dev
Open the
http://localhost:3000/
in your browser to see the result:
#Troubleshooting
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:
Could not open report ‘TestReport’
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 back-end application.
- The
reportUrl
setting value matches an existing report. For the backend application, ensure that either the Reports folder contains a reportUrl.repx file or the ReportsFactory.Reports dictionary contains the reportUrl entry (if the back-end application originated from the DevExpress template). - The version of DevExpress npm packages should match the version of NuGet packages. Enable Development Mode to check for a library version mismatch on every request to the server. For details, review the following help topic: Server-Side Libraries Version.
Refer to the following topic for more information: Troubleshooting.
#Next Steps
- Specify Parameter Values
- Specify Parameter Values through the Document Viewer’s UI or code.
- Customize the Toolbar
- Customize the Document Viewer Toolbar: hide export formats, customize toolbar commands.
- Customize the Tab Panel
- Customize the Document Viewer Tab Panel: remove the Tsb Panel, add new tabs to the Panel. Customize Parameter Editor : Customize a standard parameter editor in the Web Document Viewer.