Sphere
Log in Join

Chart.js 'Canvas is already in use. Chart with ID 0 must be destroyed' in React

This one showed up only in dev, where React StrictMode double-invokes effects. Production looked fine, which of course meant it got ignored for a while. Then we enabled StrictMode in a staging build and the whole dashboard page went blank with this in the console:

Uncaught Error: Canvas is already in use. Chart with ID '0' must be destroyed
before the canvas can be reused.
at Chart.buildOrUpdateControllers (chart.cjs:8234:13)
at new Chart (chart.cjs:8421:10)
at Object.useEffect (SnapshotChart.jsx:18:24)

Chart.js 4.4.1, React 18.2, Vite 5.2. The component created a chart in useEffect and never destroyed it.

## What the error is actually saying

Chart.js keeps a registry of charts keyed by canvas element. When you call new Chart(ctx, config), it looks up the canvas and refuses to attach a second controller if one is already registered. The ID in the message is just the registry counter, not anything meaningful.

React StrictMode is what surfaced it. In dev, StrictMode mounts a component, runs its effects, cleans up, then mounts it again to check that cleanup is implemented. If your effect creates a chart and the cleanup function is missing, the second mount tries to reuse the same canvas. Hence the error.

So the bug was real before StrictMode; StrictMode just made it visible. That's a decent argument for leaving StrictMode on.

## The cleanup that fixes it

Return a function from the effect that destroys the chart:

jsx
import { useEffect, useRef } from "react";
import { Chart, LineController, LineElement, PointElement, CategoryScale, LinearScale } from "chart.js";

Chart.register(LineController, LineElement, PointElement, CategoryScale, LinearScale);

export default function SnapshotChart({ data }) {
const canvasRef = useRef(null);

useEffect(() => {
const chart = new Chart(canvasRef.current, {
type: "line",
data,
options: { responsive: true },
});
return () => chart.destroy();
}, [data]);

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

The dependency array matters as much as the cleanup. With [data], a data change destroys and recreates the chart. With [], you'd need chart.data = data; chart.update() inside the effect instead, which is cheaper but more code. We went with destroy-and-recreate because our data updates were infrequent.

## The React 18 gotcha with refs

One subtle thing: if the canvas is conditionally rendered, canvasRef.current can be null when the effect runs. Guard it:

js
if (!canvasRef.current) return;

Without that you get Cannot read properties of null (reading 'getContext'), which is a different error but equally confusing at 11pm.

## If you're using react-chartjs-2

The wrapper handles the registry for you, so you shouldn't see this at all. If you do, it usually means you mixed approaches — one component uses <Line /> and another calls new Chart() on the same canvas by hand. Pick one. We had exactly that in a chart that had been half-migrated, and deleting the imperative version fixed it.

Anyway, chart.destroy() in the effect cleanup is the whole fix. It's one line, and one of those things that's invisible until StrictMode or a fast refresh makes it fail.