Web components are not supported in server-side rendering (SSR) environments. This example assumes that your React application is running entirely on the browser (client-side only). If you attempt to use the chart component library within SSR frameworks (such as Next.js or Gatsby) without proper dynamic import or client-side rendering guards, you may encounter errors.
Instantiating the Nebuly Charts Client
To get started, you’ll first need to instantiate the Nebuly Charts client.
The following code snippet shows how to do this by importing the library and creating a client instance using your API key:
Providing the Nebuly Client via React Context
Next, it’s best practice in React to provide the Nebuly client throughout your app using React context.
This lets any component access the client instance via a custom hook, without having to pass it through props manually.
Here’s how you can create a provider component for the Nebuly client and a corresponding hook:
Registering Nebuly Web Components in React
To use Nebuly web components (such as charts) in your React application, you need to register them once—usually in your main entry point file.
This enables the custom HTML elements to be recognized by the browser.
The snippet below demonstrates how to do this within a React application setup:
Displaying a List of Reports
If you want to display a list of available reports fetched from the Nebuly API, you can define a simple component that leverages the client, uses a data-fetching library like React Query, and handles loading and error states gracefully.
Here’s an example implementation:
Rendering a Report and Its Charts
Once a user selects a report, you’ll want to fetch detailed data for that report and render the associated charts.
The following example demonstrates how you might build a Report component that retrieves report data, supports loading and error handling, and renders each chart using a separate ChartComponent:
ChartComponent: Rendering Nebuly Chart Types
The ChartComponent helper function is responsible for rendering the appropriate Nebuly chart web component based on the chart type.
You can set up your chart option objects and extend the switch statement to support all the chart types you need: