Skip to content

Commit

Permalink
Fix Chart initialized in unmounted component (#3724)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mpressmar and melloware authored Nov 30, 2022
1 parent 0aba283 commit e3cff46
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions components/lib/chart/Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e3cff46

Please sign in to comment.