Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 5bd6137

Browse files
authored
Don't attempt to resize unmounted graphs (#563)
* Don't attempt to resize unmounted graphs Fixes #534 * Anon resize function -> named bound function; add removeEventListener * Add additional gd conditional in plot() * Add integration test for resizing unmounted graphs * Add changelog entry for PR #563 (Don't attempt to resize unmounted graphs) * black for test_integration.py * CHANGELOG ticks
1 parent 6be44da commit 5bd6137

File tree

3 files changed

+2438
-2329
lines changed

3 files changed

+2438
-2329
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55
## [Unreleased]
66
### Fixed
77
- Fixed `min_date_allowed` and `max_date_allowed` bug in `DatePickerRange` [#551]https://github.com/plotly/dash-core-components/issues/551)
8+
- Fixed unwanted `resize()` calls on unmounted `Graph`s [#534](https://github.com/plotly/dash-core-components/issues/534)
89

910
### Changed
1011
- Changed `dcc.Checklist` prop `values` to `value`, to match all the other input components [#558](https://github.com/plotly/dash-core-components/pull/558). Also improved prop types for `Dropdown` and `RadioItems` `value` props to consistently accept both strings and numbers.

src/components/Graph.react.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class PlotlyGraph extends Component {
9191
super(props);
9292
this.bindEvents = this.bindEvents.bind(this);
9393
this._hasPlotted = false;
94+
this.graphResize = this.graphResize.bind(this);
9495
}
9596

9697
plot(props) {
@@ -111,9 +112,13 @@ class PlotlyGraph extends Component {
111112
config: config,
112113
}).then(() => {
113114
if (!this._hasPlotted) {
114-
this.bindEvents();
115-
Plotly.Plots.resize(document.getElementById(id));
116-
this._hasPlotted = true;
115+
// double-check gd hasn't been unmounted
116+
const gd = document.getElementById(id);
117+
if (gd) {
118+
this.bindEvents();
119+
Plotly.Plots.resize(gd);
120+
this._hasPlotted = true;
121+
}
117122
}
118123
});
119124
}
@@ -141,6 +146,13 @@ class PlotlyGraph extends Component {
141146
return Plotly.extendTraces(id, updateData, traceIndices, maxPoints);
142147
}
143148

149+
graphResize() {
150+
const graphDiv = document.getElementById(this.props.id);
151+
if (graphDiv) {
152+
Plotly.Plots.resize(graphDiv);
153+
}
154+
}
155+
144156
bindEvents() {
145157
const {id, setProps, clear_on_unhover} = this.props;
146158

@@ -195,16 +207,15 @@ class PlotlyGraph extends Component {
195207

196208
componentDidMount() {
197209
this.plot(this.props).then(() => {
198-
window.addEventListener('resize', () => {
199-
Plotly.Plots.resize(document.getElementById(this.props.id));
200-
});
210+
window.addEventListener('resize', this.graphResize);
201211
});
202212
}
203213

204214
componentWillUnmount() {
205215
if (this.eventEmitter) {
206216
this.eventEmitter.removeAllListeners();
207217
}
218+
window.removeEventListener('resize', this.graphResize);
208219
}
209220

210221
shouldComponentUpdate(nextProps) {

0 commit comments

Comments
 (0)