Skip to content

Commit b970cdd

Browse files
committed
Output a warning whenever user tries to use datasets without a unique key
1 parent 459504b commit b970cdd

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import isEqual from 'lodash.isequal';
66

77
class ChartComponent extends React.Component {
88
static getLabelAsKey = d => d.label;
9-
9+
1010
static propTypes = {
1111
data: PropTypes.oneOfType([
1212
PropTypes.object,
@@ -155,8 +155,14 @@ class ChartComponent extends React.Component {
155155
// use the key provider to work out which series have been added/removed/changed
156156
const currentDatasetKeys = currentDatasets.map(this.props.datasetKeyProvider);
157157
const nextDatasetKeys = nextDatasets.map(this.props.datasetKeyProvider);
158+
const shouldWarn = !currentDatasetKeys.every(d => typeof d !== "undefined") || !nextDatasetKeys.every(d => typeof d !== "undefined");
158159
const newDatasets = nextDatasets.filter(d => currentDatasetKeys.indexOf(this.props.datasetKeyProvider(d)) === -1);
159160

161+
if (shouldWarn && !this.hasWarned) {
162+
this.hasWarned = true; // Only warn once per chart so console isn't spammed with warnings
163+
console.error('[react-chartjs-2] Warning: Each dataset need 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.');
164+
}
165+
160166
// process the updates (via a reverse for loop so we can safely splice deleted datasets out of the array
161167
for (let idx = currentDatasets.length - 1; idx >= 0; idx -= 1) {
162168
const currentDatasetKey = this.props.datasetKeyProvider(currentDatasets[idx]);

0 commit comments

Comments
 (0)