Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read properties of null (reading 'addEventListener') #40

Open
nfort opened this issue Dec 21, 2023 · 4 comments
Open

Cannot read properties of null (reading 'addEventListener') #40

nfort opened this issue Dec 21, 2023 · 4 comments

Comments

@nfort
Copy link

nfort commented Dec 21, 2023

Hi, i have got the error in the console

import { useEffect, useRef } from "react";
import Chart from "chart.js/auto";
import "chartjs-adapter-date-fns";
import ChartDatasourcePrometheusPlugin from "chartjs-plugin-datasource-prometheus";
import "./styles.css";

export default function App() {
  const canvasRef = useRef<HTMLCanvasElement>(null);
  const chartRef = useRef<Chart | null>(null);

  useEffect(() => {
    if (canvasRef.current) {
      chartRef.current = new Chart(canvasRef.current, {
        type: "line",
        data: {
          datasets: [],
        },
        plugins: [ChartDatasourcePrometheusPlugin],
        options: {
          plugins: {
            "datasource-prometheus": {
              prometheus: {
                endpoint: "https://prometheus.demo.do.prometheus.io",
                baseURL: "/api/v1", // default value
              },
              query: "sum by (job) (go_gc_duration_seconds)",
              timeRange: {
                type: "relative",
                // from 1 hours ago to now
                start: -1 * 60 * 60 * 1000,
                end: 0,
              },
            },
          },
        },
      });
    }

    return () => {
      chartRef.current?.destroy();
    };
  }, []);

  return <canvas ref={canvasRef}></canvas>;
}

Playground:
https://codesandbox.io/p/sandbox/chartjs-prometheus-bug-4jq97s

@samber
Copy link
Owner

samber commented Dec 21, 2023

This error seems to be front chartjs. Did you search in their issues ?

@nfort
Copy link
Author

nfort commented Dec 23, 2023

@samber yes, i found only one issue, but it seems not relevant here

chartjs/Chart.js#11295

@samber
Copy link
Owner

samber commented Dec 23, 2023

Do you try react-chartjs-2 ?

You're code seems good, but this library might be handling some special cases...

@nfort
Copy link
Author

nfort commented Dec 25, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants