From e3cff46bf06b1db443cd14141ddb98909b7ba566 Mon Sep 17 00:00:00 2001 From: mpressmar Date: Wed, 30 Nov 2022 20:20:51 +0100 Subject: [PATCH] Fix Chart initialized in unmounted component (#3724) * Fix Chart initialized in unmounted component The `initChart` method will in some case asynchronously create a chart object (after loading `chart.js/auto`). If during the asynchronous process the Chart component was already unmounted, the Chart will still be created, but for a `null` canvas. As a consequence, a zombie Chart is created that is never cleaned up. * Update Chart.js Co-authored-by: Melloware --- components/lib/chart/Chart.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/components/lib/chart/Chart.js b/components/lib/chart/Chart.js index efda749004..b722127abe 100644 --- a/components/lib/chart/Chart.js +++ b/components/lib/chart/Chart.js @@ -34,6 +34,12 @@ const PrimeReactChart = React.memo( import('chart.js/auto').then((module) => { destroyChart(); + // In case that the Chart component has been unmounted during asynchronous loading of ChartJS, + // the canvasRef will not be available anymore, and no Chart should be created. + if (!canvasRef.current) { + return; + } + if (module) { if (module.default) { // WebPack