Skip to content

Commit b1eb63f

Browse files
committed
fix(react): Evade unnecessary heavy chart renders
1 parent 8f4f859 commit b1eb63f

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

packages/cubejs-playground/src/scaffolding/react/components/ChartRenderer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const renderChart = (Component) => ({ resultSet, error }) => (
1313

1414
const ChartRenderer = ({ vizState, cubejsApi }) => vizState && (
1515
<QueryRenderer
16+
updateOnlyOnStateChange
1617
query={vizState.query}
1718
cubejsApi={cubejsApi}
1819
render={

packages/cubejs-react/dist/cubejs-react.esm.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ function (_React$Component) {
7272
queries = _this$props2.queries,
7373
render = _this$props2.render,
7474
cubejsApi = _this$props2.cubejsApi,
75-
loadSql = _this$props2.loadSql;
76-
return !equals(nextProps.query, query) || !equals(nextProps.queries, queries) || (nextProps.render == null || render == null) && nextProps.render !== render || nextProps.cubejsApi !== cubejsApi || nextProps.loadSql !== loadSql || !equals(nextState, this.state);
75+
loadSql = _this$props2.loadSql,
76+
updateOnlyOnStateChange = _this$props2.updateOnlyOnStateChange;
77+
78+
if (!updateOnlyOnStateChange) {
79+
return true;
80+
}
81+
82+
return !equals(nextProps.query, query) || !equals(nextProps.queries, queries) || (nextProps.render == null || render == null) && nextProps.render !== render || nextProps.cubejsApi !== cubejsApi || nextProps.loadSql !== loadSql || !equals(nextState, this.state) || nextProps.updateOnlyOnStateChange !== updateOnlyOnStateChange;
7783
}
7884
}, {
7985
key: "componentDidUpdate",
@@ -239,13 +245,15 @@ QueryRenderer.propTypes = {
239245
cubejsApi: object.isRequired,
240246
query: object,
241247
queries: object,
242-
loadSql: any
248+
loadSql: any,
249+
updateOnlyOnStateChange: bool
243250
};
244251
QueryRenderer.defaultProps = {
245252
query: null,
246253
render: null,
247254
queries: null,
248-
loadSql: null
255+
loadSql: null,
256+
updateOnlyOnStateChange: false
249257
};
250258

251259
var QueryRendererWithTotals = function QueryRendererWithTotals(_ref) {

packages/cubejs-react/dist/cubejs-react.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,14 @@ function (_React$Component) {
7878
queries = _this$props2.queries,
7979
render = _this$props2.render,
8080
cubejsApi = _this$props2.cubejsApi,
81-
loadSql = _this$props2.loadSql;
82-
return !ramda.equals(nextProps.query, query) || !ramda.equals(nextProps.queries, queries) || (nextProps.render == null || render == null) && nextProps.render !== render || nextProps.cubejsApi !== cubejsApi || nextProps.loadSql !== loadSql || !ramda.equals(nextState, this.state);
81+
loadSql = _this$props2.loadSql,
82+
updateOnlyOnStateChange = _this$props2.updateOnlyOnStateChange;
83+
84+
if (!updateOnlyOnStateChange) {
85+
return true;
86+
}
87+
88+
return !ramda.equals(nextProps.query, query) || !ramda.equals(nextProps.queries, queries) || (nextProps.render == null || render == null) && nextProps.render !== render || nextProps.cubejsApi !== cubejsApi || nextProps.loadSql !== loadSql || !ramda.equals(nextState, this.state) || nextProps.updateOnlyOnStateChange !== updateOnlyOnStateChange;
8389
}
8490
}, {
8591
key: "componentDidUpdate",
@@ -245,13 +251,15 @@ QueryRenderer.propTypes = {
245251
cubejsApi: PropTypes.object.isRequired,
246252
query: PropTypes.object,
247253
queries: PropTypes.object,
248-
loadSql: PropTypes.any
254+
loadSql: PropTypes.any,
255+
updateOnlyOnStateChange: PropTypes.bool
249256
};
250257
QueryRenderer.defaultProps = {
251258
query: null,
252259
render: null,
253260
queries: null,
254-
loadSql: null
261+
loadSql: null,
262+
updateOnlyOnStateChange: false
255263
};
256264

257265
var QueryRendererWithTotals = function QueryRendererWithTotals(_ref) {

packages/cubejs-react/dist/cubejs-react.umd.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6842,8 +6842,14 @@
68426842
queries = _this$props2.queries,
68436843
render = _this$props2.render,
68446844
cubejsApi = _this$props2.cubejsApi,
6845-
loadSql = _this$props2.loadSql;
6846-
return !equals(nextProps.query, query) || !equals(nextProps.queries, queries) || (nextProps.render == null || render == null) && nextProps.render !== render || nextProps.cubejsApi !== cubejsApi || nextProps.loadSql !== loadSql || !equals(nextState, this.state);
6845+
loadSql = _this$props2.loadSql,
6846+
updateOnlyOnStateChange = _this$props2.updateOnlyOnStateChange;
6847+
6848+
if (!updateOnlyOnStateChange) {
6849+
return true;
6850+
}
6851+
6852+
return !equals(nextProps.query, query) || !equals(nextProps.queries, queries) || (nextProps.render == null || render == null) && nextProps.render !== render || nextProps.cubejsApi !== cubejsApi || nextProps.loadSql !== loadSql || !equals(nextState, this.state) || nextProps.updateOnlyOnStateChange !== updateOnlyOnStateChange;
68476853
}
68486854
}, {
68496855
key: "componentDidUpdate",
@@ -7009,13 +7015,15 @@
70097015
cubejsApi: PropTypes.object.isRequired,
70107016
query: PropTypes.object,
70117017
queries: PropTypes.object,
7012-
loadSql: PropTypes.any
7018+
loadSql: PropTypes.any,
7019+
updateOnlyOnStateChange: PropTypes.bool
70137020
};
70147021
QueryRenderer.defaultProps = {
70157022
query: null,
70167023
render: null,
70177024
queries: null,
7018-
loadSql: null
7025+
loadSql: null,
7026+
updateOnlyOnStateChange: false
70197027
};
70207028

70217029
var QueryRendererWithTotals = function QueryRendererWithTotals(_ref) {

packages/cubejs-react/src/QueryRenderer.jsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ export default class QueryRenderer extends React.Component {
2626

2727
shouldComponentUpdate(nextProps, nextState) {
2828
const {
29-
query, queries, render, cubejsApi, loadSql
29+
query, queries, render, cubejsApi, loadSql, updateOnlyOnStateChange
3030
} = this.props;
31+
if (!updateOnlyOnStateChange) {
32+
return true;
33+
}
3134
return !equals(nextProps.query, query)
3235
|| !equals(nextProps.queries, queries)
3336
|| ((nextProps.render == null || render == null) && nextProps.render !== render)
3437
|| nextProps.cubejsApi !== cubejsApi
3538
|| nextProps.loadSql !== loadSql
36-
|| !equals(nextState, this.state);
39+
|| !equals(nextState, this.state)
40+
|| nextProps.updateOnlyOnStateChange !== updateOnlyOnStateChange;
3741
}
3842

3943
componentDidUpdate(prevProps) {
@@ -113,12 +117,14 @@ QueryRenderer.propTypes = {
113117
cubejsApi: PropTypes.object.isRequired,
114118
query: PropTypes.object,
115119
queries: PropTypes.object,
116-
loadSql: PropTypes.any
120+
loadSql: PropTypes.any,
121+
updateOnlyOnStateChange: PropTypes.bool
117122
};
118123

119124
QueryRenderer.defaultProps = {
120125
query: null,
121126
render: null,
122127
queries: null,
123-
loadSql: null
128+
loadSql: null,
129+
updateOnlyOnStateChange: false
124130
};

0 commit comments

Comments
 (0)