How to Use a Javascript Reporting Tool with Next.js
Next.js is a React-based framework that provides a well-defined structure for your application and optimizations that make the development process and final application faster. Unlike classic front-end frameworks, such as Angular or Vue, Next.js supports Pre-Rendering of the application’s pages at runtime on the server-side or at building time.
ActiveReportsJS components need a browser environment and cannot work on the server-side or at the building time. However, it’s certainly possible to use ActiveReportsJS components within a Next.js application. This article describes the approach to getting it done.
Using Components Wrappers
Suppose you want to use ActiveReportsJS Report Viewer or Report Designer component in a Next.js application. Implementing a wrapper that encapsulates the component’s functionality and exposes an interface to consume it could be convenient. For example, here is the code of the Report Viewer’s wrapper that exposes, in addition to the built-in Viewer’s properties, the reportUri property that points to a report to be loaded.
Similarly, the wrapper for the Report Designer component could look like the following
Using Wrappers for API Invocation
Suppose you want to use the ActiveReportsJS API. In that case, it could also be encapsulated within a wrapper that does not render any UI but simply tracks properties' changes and calls the API functions. Here is an example of the PDFExport wrapper:
Dynamic Loading of Wrappers
Next.js contains the Dynamic Import feature that allows loading components at runtime and prevents server-side rendering. This is required for using ActiveReportsJS components within a Next.js application. Below is the sample of the Next.js Page that displays the Report Viewer component and loads the Products.rdlx-json report into it. The code assumes the wrapper’s code is in the components\ReportViewer.tsx file.
You could use the same approach for API wrappers: