Skip to content

Commit 183d8b3

Browse files
committed
Fix error when trying to plot after component unmounted
1 parent 5bb2d18 commit 183d8b3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/factory.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,20 @@ export default function plotComponentFactory(Plotly) {
6464
}
6565

6666
componentDidMount() {
67+
this.unmounting = false;
68+
6769
this.p = this.p
6870
.then(() => {
71+
if (!this.el) {
72+
let error;
73+
if (this.unmounting) {
74+
error = new Error('Component is unmounting');
75+
error.reason = 'unmounting';
76+
} else {
77+
error = new Error('Missing element reference');
78+
}
79+
throw error;
80+
}
6981
return Plotly.newPlot(this.el, {
7082
data: this.props.data,
7183
layout: this.props.layout,
@@ -78,6 +90,9 @@ export default function plotComponentFactory(Plotly) {
7890
.then(this.attachUpdateEvents)
7991
.then(() => this.figureCallback(this.props.onInitialized))
8092
.catch(err => {
93+
if (err.reason === 'unmounting') {
94+
return;
95+
}
8196
console.error('Error while plotting:', err); // eslint-disable-line no-console
8297
if (this.props.onError) {
8398
this.props.onError(err);
@@ -86,6 +101,8 @@ export default function plotComponentFactory(Plotly) {
86101
}
87102

88103
componentWillUpdate(nextProps) {
104+
this.unmounting = false;
105+
89106
if (nextProps.revision !== void 0 && nextProps.revision === this.props.revision) {
90107
// if revision is set and unchanged, do nothing
91108
return;
@@ -108,6 +125,16 @@ export default function plotComponentFactory(Plotly) {
108125

109126
this.p = this.p
110127
.then(() => {
128+
if (!this.el) {
129+
let error;
130+
if (this.unmounting) {
131+
error = new Error('Component is unmounting');
132+
error.reason = 'unmounting';
133+
} else {
134+
error = new Error('Missing element reference');
135+
}
136+
throw error;
137+
}
111138
return Plotly.react(this.el, {
112139
data: nextProps.data,
113140
layout: nextProps.layout,
@@ -119,6 +146,9 @@ export default function plotComponentFactory(Plotly) {
119146
.then(() => this.syncWindowResize(nextProps))
120147
.then(() => this.figureCallback(nextProps.onUpdate))
121148
.catch(err => {
149+
if (err.reason === 'unmounting') {
150+
return;
151+
}
122152
console.error('Error while plotting:', err); // eslint-disable-line no-console
123153
if (this.props.onError) {
124154
this.props.onError(err);
@@ -127,6 +157,8 @@ export default function plotComponentFactory(Plotly) {
127157
}
128158

129159
componentWillUnmount() {
160+
this.unmounting = true;
161+
130162
this.figureCallback(this.props.onPurge);
131163

132164
if (this.resizeHandler && isBrowser) {

0 commit comments

Comments
 (0)