Skip to content

Commit ea08fa1

Browse files
committed
only show warning in dev enviroment
1 parent 70e91d9 commit ea08fa1

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ import ReactDOM from 'react-dom';
44
import Chart from 'chart.js';
55
import isEqual from 'lodash.isequal';
66

7+
// Gets React's enviroment
8+
// https://stackoverflow.com/a/25922668
9+
const env = (() => {
10+
try {
11+
React.createClass({});
12+
} catch(e) {
13+
if (e.message.indexOf('render') >= 0) {
14+
return 'dev'; // A nice, specific error message
15+
} else {
16+
return 'prod'; // A generic error message
17+
}
18+
}
19+
return 'prod'; // should never happen, but play it safe.
20+
})();
21+
722
class ChartComponent extends React.Component {
823
static getLabelAsKey = d => d.label;
924

@@ -155,10 +170,10 @@ class ChartComponent extends React.Component {
155170
// use the key provider to work out which series have been added/removed/changed
156171
const currentDatasetKeys = currentDatasets.map(this.props.datasetKeyProvider);
157172
const nextDatasetKeys = nextDatasets.map(this.props.datasetKeyProvider);
158-
const shouldWarn = !currentDatasetKeys.every(d => typeof d !== "undefined") || !nextDatasetKeys.every(d => typeof d !== "undefined");
173+
const shouldWarn = !currentDatasetKeys.every(d => typeof d !== 'undefined') || !nextDatasetKeys.every(d => typeof d !== 'undefined');
159174
const newDatasets = nextDatasets.filter(d => currentDatasetKeys.indexOf(this.props.datasetKeyProvider(d)) === -1);
160175

161-
if (shouldWarn && !this.hasWarned) {
176+
if (shouldWarn && !this.hasWarned && env === 'dev') {
162177
this.hasWarned = true; // Only warn once per chart so console isn't spammed with warnings
163178
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.');
164179
}

0 commit comments

Comments
 (0)