React-chartjs-2: ChartJS mutates the state

Created on 7 Feb 2018  路  6Comments  路  Source: reactchartjs/react-chartjs-2

After passing the chart data to the component for rendering, the chart.js graph mutates the data object and creates a recursive problem when doing a JSON.stringify to the data object. It is easy to recreate the problem. after the chart has rendered try and stringify the data object.

Most helpful comment

Any update on this?

Here's a quick workaround. If you're passing a data variable to react-chartjs-2, do this first:

data = JSON.parse(JSON.stringify(data));

We did this on my project. It's obviously not good for performance, but it works.

All 6 comments

Can you post an simple example? I'm not a developer on this project, but I'm seeing something similar and I'm wondering if we're hitting the same issue.

I've put together a simple demo of this bug. See the project README for instructions for installing and running the demo:

https://github.com/openjck/bug-demo-react-chartjs-2-mutates-state

Any update on this?

Here's a quick workaround. If you're passing a data variable to react-chartjs-2, do this first:

data = JSON.parse(JSON.stringify(data));

We did this on my project. It's obviously not good for performance, but it works.

This is this the same workaround I ended up using. I ended up holding two copies of the data one that i would keep up to date with RactJS and one that I would pass to React-Chartjs. The issue is that react-chartjs needs to graph the chart without mutating the data.

This is a known issue with chart.js.

https://github.com/jerairrest/react-chartjs-2/blob/master/src/index.js#L125-L128

I'm not entirely sure what we can do about this since it's the core of chart.js is making this change.

Closing for now. If this problem persists with the latest versions of both libraries then please feel free to reopen

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidcalhoun picture davidcalhoun  路  5Comments

DavidSongzw picture DavidSongzw  路  5Comments

ekobayu picture ekobayu  路  5Comments

pkellner picture pkellner  路  4Comments

jbbae picture jbbae  路  5Comments