Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import isEqual from 'lodash.isequal';

class ChartComponent extends React.Component {
static getLabelAsKey = d => d.label;

static propTypes = {
data: PropTypes.oneOfType([
PropTypes.object,
Expand Down Expand Up @@ -155,8 +155,14 @@ class ChartComponent extends React.Component {
// use the key provider to work out which series have been added/removed/changed
const currentDatasetKeys = currentDatasets.map(this.props.datasetKeyProvider);
const nextDatasetKeys = nextDatasets.map(this.props.datasetKeyProvider);
const shouldWarn = !currentDatasetKeys.every(d => typeof d !== "undefined") || !nextDatasetKeys.every(d => typeof d !== "undefined");
const newDatasets = nextDatasets.filter(d => currentDatasetKeys.indexOf(this.props.datasetKeyProvider(d)) === -1);

if (shouldWarn && !this.hasWarned) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should only occur if you are not in a production build. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I check if production vs. development?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this will work unless you have a better method https://stackoverflow.com/a/25922668

this.hasWarned = true; // Only warn once per chart so console isn't spammed with warnings
console.error('[react-chartjs-2] Warning: Each dataset needs a unique key. By default, the "label" property on each dataset is used. Alternatively, you may provide a "datasetKeyProvider" as a prop that returns a unique key.');
}

// process the updates (via a reverse for loop so we can safely splice deleted datasets out of the array
for (let idx = currentDatasets.length - 1; idx >= 0; idx -= 1) {
const currentDatasetKey = this.props.datasetKeyProvider(currentDatasets[idx]);
Expand Down