|
1 | 1 | import * as React from 'react';
|
2 | 2 | import GoogleAnalyticsContext, { GoogleAnalyticsState } from './GoogleAnalyticsContext';
|
| 3 | +import equal from 'react-fast-compare'; |
3 | 4 |
|
4 | 5 | export type DataChartProps<T> = {
|
5 | 6 | /** Query */
|
@@ -33,25 +34,40 @@ class DataChart<O> extends React.Component<DataChartProps<O>> {
|
33 | 34 | }
|
34 | 35 |
|
35 | 36 | componentDidMount(): void {
|
36 |
| - this.componentDidUpdate(); |
| 37 | + this.componentDidUpdate({ query: null } as any); |
37 | 38 | }
|
38 | 39 |
|
39 |
| - componentDidUpdate(): void { |
| 40 | + componentDidUpdate(prevProps: DataChartProps<O>): void { |
40 | 41 | const gaState = this.context as GoogleAnalyticsState;
|
41 | 42 | const { query, children, style, className, onSuccess, onError, ...chartOptions } = this.props;
|
42 | 43 | // Rendering the component only if a user authenticated
|
43 | 44 | if (gaState === 'AUTH_SUCCESS') {
|
44 | 45 | // Updating the existing chart with new options if already rendered
|
45 | 46 | if (this.googleDataChart != null) {
|
46 |
| - this.googleDataChart.set({ |
47 |
| - query, |
48 |
| - chart: { |
49 |
| - container: this.elementRef.current as HTMLElement, |
50 |
| - type: this.chartType as any, |
51 |
| - options: chartOptions as any |
52 |
| - } |
53 |
| - }); |
54 |
| - this.googleDataChart.execute(); |
| 47 | + const { |
| 48 | + query: prevQuery, |
| 49 | + children: _children, |
| 50 | + style: _style, |
| 51 | + className: _className, |
| 52 | + onSuccess: _onSuccess, |
| 53 | + onError: _onError, |
| 54 | + ...prevChartOptions |
| 55 | + } = prevProps; |
| 56 | + |
| 57 | + if ( |
| 58 | + prevQuery != null && |
| 59 | + (!equal(prevQuery, query) || !equal(prevChartOptions, chartOptions)) |
| 60 | + ) { |
| 61 | + this.googleDataChart.set({ |
| 62 | + query, |
| 63 | + chart: { |
| 64 | + container: this.elementRef.current as HTMLElement, |
| 65 | + type: this.chartType as any, |
| 66 | + options: chartOptions as any |
| 67 | + } |
| 68 | + }); |
| 69 | + this.googleDataChart.execute(); |
| 70 | + } |
55 | 71 | } else {
|
56 | 72 | // Creating a chart if a chart instance not created
|
57 | 73 | this.googleDataChart = new gapi.analytics.googleCharts.DataChart({
|
|
0 commit comments