react-google-charts icon indicating copy to clipboard operation
react-google-charts copied to clipboard

Console error about state update on unmounted Chart component

Open gyoung-panopto opened this issue 5 years ago • 8 comments

We are seeing a console error when using the Chart component. This happens when the Chart is unmounted and replaced with a different chart component. Any ideas why this might be happening or how to work around/suppress the error?

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method. in Chart (created by ViewsChart)

(ViewsChart is our custom component)

gyoung-panopto avatar Dec 10 '18 22:12 gyoung-panopto

Same here.

ljubic avatar Dec 14 '18 11:12 ljubic

This is happening with Bar chart in particular.

ravichandrareddyl avatar Dec 18 '18 03:12 ravichandrareddyl

This happens for me with the Pie Chart. Thanks for your great work though 💯

odinolav avatar Jan 29 '19 06:01 odinolav

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 30 '19 06:03 stale[bot]

Same problem for me too :(

sintanial avatar Apr 03 '19 06:04 sintanial

Can anyone create a reproduction of this problem on CodeSandbox ?

Having trouble reproducing it.

rakannimer avatar Apr 03 '19 08:04 rakannimer

I tried reproducing this on CodeSandbox and was unable to. So I started investigating in my project locally.

I realized that the parent component where I was rendering my Chart was re-rendering several times on unmount and this render thrashing was manifesting itself in this issue. When I fixed my project so I don't have these re-renders, I no longer see this error.

Is anyone seeing this error who has a clean unmount cycle w/o extra re-renders?

melanievee avatar Apr 04 '19 22:04 melanievee

The problem is in useLoadGoogleCharts() and can happen if the component unloads before the charts API is fully loaded.

google.charts.load(chartVersion, {
        packages: chartPackages,
        language: chartLanguage,
        mapsApiKey,
      });
google.charts.setOnLoadCallback(() => {
       setGoogleCharts(google);
});

The callback setOnLoadCallback may complete after component unload and setGoogleCharts will then trigger the error.

This causes other instability issues where the charts API then refuses to load and you get blank charts.

The solution is exactly as the error message describes. The useEffect cleanup function should set some cancel flag that should be checked in the setOnLoadCallback before attempting to assign state.

aarononeal avatar Mar 12 '22 03:03 aarononeal