Skip to content

Commit 9ab3e88

Browse files
committed
Remove deprecated react methods; refactor
1 parent 44b2bd5 commit 9ab3e88

File tree

1 file changed

+45
-70
lines changed

1 file changed

+45
-70
lines changed

src/factory.js

+45-70
Original file line numberDiff line numberDiff line change
@@ -61,48 +61,10 @@ export default function plotComponentFactory(Plotly) {
6161
this.getRef = this.getRef.bind(this);
6262
this.handleUpdate = this.handleUpdate.bind(this);
6363
this.figureCallback = this.figureCallback.bind(this);
64+
this.updatePlotly = this.updatePlotly.bind(this);
6465
}
6566

66-
componentDidMount() {
67-
this.unmounting = false;
68-
69-
this.p = this.p
70-
.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-
}
81-
return Plotly.newPlot(this.el, {
82-
data: this.props.data,
83-
layout: this.props.layout,
84-
config: this.props.config,
85-
frames: this.props.frames,
86-
});
87-
})
88-
.then(() => this.syncWindowResize(null, true))
89-
.then(this.syncEventHandlers)
90-
.then(this.attachUpdateEvents)
91-
.then(() => this.figureCallback(this.props.onInitialized))
92-
.catch(err => {
93-
if (err.reason === 'unmounting') {
94-
return;
95-
}
96-
console.error('Error while plotting:', err); // eslint-disable-line no-console
97-
if (this.props.onError) {
98-
this.props.onError(err);
99-
}
100-
});
101-
}
102-
103-
UNSAFE_componentWillUpdate(nextProps) {
104-
this.unmounting = false;
105-
67+
shouldComponentUpdate(nextProps) {
10668
// frames *always* changes identity so fall back to check length only :(
10769
const numPrevFrames =
10870
this.props.frames && this.props.frames.length ? this.props.frames.length : 0;
@@ -118,10 +80,15 @@ export default function plotComponentFactory(Plotly) {
11880
const revisionDefined = nextProps.revision !== void 0;
11981
const revisionChanged = nextProps.revision !== this.props.revision;
12082

121-
if (!figureChanged && (!revisionDefined || (revisionDefined && !revisionChanged))) {
122-
return;
123-
}
83+
return figureChanged || (revisionDefined && revisionChanged);
84+
}
12485

86+
updatePlotly(
87+
props,
88+
shouldInvokeResizeHandler,
89+
figureCallbackFunction,
90+
shouldAttachUpdateEvents
91+
) {
12592
this.p = this.p
12693
.then(() => {
12794
if (!this.el) {
@@ -135,26 +102,39 @@ export default function plotComponentFactory(Plotly) {
135102
throw error;
136103
}
137104
return Plotly.react(this.el, {
138-
data: nextProps.data,
139-
layout: nextProps.layout,
140-
config: nextProps.config,
141-
frames: nextProps.frames,
105+
data: props.data,
106+
layout: props.layout,
107+
config: props.config,
108+
frames: props.frames,
142109
});
143110
})
144-
.then(() => this.syncEventHandlers(nextProps))
145-
.then(() => this.syncWindowResize(nextProps))
146-
.then(() => this.figureCallback(nextProps.onUpdate))
111+
.then(() => this.syncWindowResize(props, shouldInvokeResizeHandler))
112+
.then(() => this.syncEventHandlers(props))
113+
.then(() => this.figureCallback(figureCallbackFunction))
114+
.then(shouldAttachUpdateEvents ? this.attachUpdateEvents : () => {})
147115
.catch(err => {
148116
if (err.reason === 'unmounting') {
149117
return;
150118
}
151119
console.error('Error while plotting:', err); // eslint-disable-line no-console
152-
if (this.props.onError) {
153-
this.props.onError(err);
120+
if (props.onError) {
121+
props.onError(err);
154122
}
155123
});
156124
}
157125

126+
componentDidMount() {
127+
this.unmounting = false;
128+
129+
this.updatePlotly(this.props, true, this.props.onInitialized, true);
130+
}
131+
132+
componentDidUpdate(nextProps) {
133+
this.unmounting = false;
134+
135+
this.updatePlotly(nextProps, false, nextProps.onUpdate, false);
136+
}
137+
158138
componentWillUnmount() {
159139
this.unmounting = true;
160140

@@ -175,19 +155,19 @@ export default function plotComponentFactory(Plotly) {
175155
return;
176156
}
177157

178-
for (let i = 0; i < updateEvents.length; i++) {
179-
this.el.on(updateEvents[i], this.handleUpdate);
180-
}
158+
updateEvents.forEach(updateEvent => {
159+
this.el.on(updateEvent, this.handleUpdate);
160+
});
181161
}
182162

183163
removeUpdateEvents() {
184164
if (!this.el || !this.el.removeListener) {
185165
return;
186166
}
187167

188-
for (let i = 0; i < updateEvents.length; i++) {
189-
this.el.removeListener(updateEvents[i], this.handleUpdate);
190-
}
168+
updateEvents.forEach(updateEvent => {
169+
this.el.removeListener(updateEvent, this.handleUpdate);
170+
});
191171
}
192172

193173
handleUpdate() {
@@ -210,9 +190,7 @@ export default function plotComponentFactory(Plotly) {
210190
}
211191

212192
if (props.useResizeHandler && !this.resizeHandler) {
213-
this.resizeHandler = () => {
214-
return Plotly.Plots.resize(this.el);
215-
};
193+
this.resizeHandler = () => Plotly.Plots.resize(this.el);
216194
window.addEventListener('resize', this.resizeHandler);
217195
if (invoke) {
218196
this.resizeHandler();
@@ -232,12 +210,9 @@ export default function plotComponentFactory(Plotly) {
232210
}
233211

234212
// Attach and remove event handlers as they're added or removed from props:
235-
syncEventHandlers(propsIn) {
213+
syncEventHandlers(props) {
236214
// Allow use of nextProps if passed explicitly:
237-
const props = propsIn || this.props;
238-
239-
for (let i = 0; i < eventNames.length; i++) {
240-
const eventName = eventNames[i];
215+
eventNames.forEach(eventName => {
241216
const prop = props['on' + eventName];
242217
const hasHandler = Boolean(this.handlers[eventName]);
243218

@@ -249,7 +224,7 @@ export default function plotComponentFactory(Plotly) {
249224
this.el.removeListener('plotly_' + eventName.toLowerCase(), this.handlers[eventName]);
250225
delete this.handlers[eventName];
251226
}
252-
}
227+
});
253228
}
254229

255230
render() {
@@ -281,9 +256,9 @@ export default function plotComponentFactory(Plotly) {
281256
divId: PropTypes.string,
282257
};
283258

284-
for (let i = 0; i < eventNames.length; i++) {
285-
PlotlyComponent.propTypes['on' + eventNames[i]] = PropTypes.func;
286-
}
259+
eventNames.forEach(eventName => {
260+
PlotlyComponent.propTypes['on' + eventName] = PropTypes.func;
261+
});
287262

288263
PlotlyComponent.defaultProps = {
289264
debug: false,

0 commit comments

Comments
 (0)