@@ -61,11 +61,10 @@ export default function plotComponentFactory(Plotly) {
61
61
this . getRef = this . getRef . bind ( this ) ;
62
62
this . handleUpdate = this . handleUpdate . bind ( this ) ;
63
63
this . figureCallback = this . figureCallback . bind ( this ) ;
64
+ this . updatePlotly = this . updatePlotly . bind ( this ) ;
64
65
}
65
66
66
- componentDidMount ( ) {
67
- this . unmounting = false ;
68
-
67
+ updatePlotly ( shouldInvokeResizeHandler , figureCallbackFunction , shouldAttachUpdateEvents ) {
69
68
this . p = this . p
70
69
. then ( ( ) => {
71
70
if ( ! this . el ) {
@@ -78,17 +77,17 @@ export default function plotComponentFactory(Plotly) {
78
77
}
79
78
throw error ;
80
79
}
81
- return Plotly . newPlot ( this . el , {
80
+ return Plotly . react ( this . el , {
82
81
data : this . props . data ,
83
82
layout : this . props . layout ,
84
83
config : this . props . config ,
85
84
frames : this . props . frames ,
86
85
} ) ;
87
86
} )
88
- . then ( ( ) => this . syncWindowResize ( null , true ) )
87
+ . then ( ( ) => this . syncWindowResize ( shouldInvokeResizeHandler ) )
89
88
. then ( this . syncEventHandlers )
90
- . then ( this . attachUpdateEvents )
91
- . then ( ( ) => this . figureCallback ( this . props . onInitialized ) )
89
+ . then ( ( ) => this . figureCallback ( figureCallbackFunction ) )
90
+ . then ( shouldAttachUpdateEvents ? this . attachUpdateEvents : ( ) => { } )
92
91
. catch ( err => {
93
92
if ( err . reason === 'unmounting' ) {
94
93
return ;
@@ -100,59 +99,35 @@ export default function plotComponentFactory(Plotly) {
100
99
} ) ;
101
100
}
102
101
103
- UNSAFE_componentWillUpdate ( nextProps ) {
102
+ componentDidMount ( ) {
103
+ this . unmounting = false ;
104
+
105
+ this . updatePlotly ( true , this . props . onInitialized , true ) ;
106
+ }
107
+
108
+ componentDidUpdate ( prevProps ) {
104
109
this . unmounting = false ;
105
110
106
111
// frames *always* changes identity so fall back to check length only :(
107
112
const numPrevFrames =
108
- this . props . frames && this . props . frames . length ? this . props . frames . length : 0 ;
113
+ prevProps . frames && prevProps . frames . length ? prevProps . frames . length : 0 ;
109
114
const numNextFrames =
110
- nextProps . frames && nextProps . frames . length ? nextProps . frames . length : 0 ;
115
+ this . props . frames && this . props . frames . length ? this . props . frames . length : 0 ;
111
116
112
117
const figureChanged = ! (
113
- nextProps . layout === this . props . layout &&
114
- nextProps . data === this . props . data &&
115
- nextProps . config === this . props . config &&
118
+ prevProps . layout === this . props . layout &&
119
+ prevProps . data === this . props . data &&
120
+ prevProps . config === this . props . config &&
116
121
numNextFrames === numPrevFrames
117
122
) ;
118
- const revisionDefined = nextProps . revision !== void 0 ;
119
- const revisionChanged = nextProps . revision !== this . props . revision ;
123
+ const revisionDefined = prevProps . revision !== void 0 ;
124
+ const revisionChanged = prevProps . revision !== this . props . revision ;
120
125
121
126
if ( ! figureChanged && ( ! revisionDefined || ( revisionDefined && ! revisionChanged ) ) ) {
122
127
return ;
123
128
}
124
129
125
- this . p = this . p
126
- . then ( ( ) => {
127
- if ( ! this . el ) {
128
- let error ;
129
- if ( this . unmounting ) {
130
- error = new Error ( 'Component is unmounting' ) ;
131
- error . reason = 'unmounting' ;
132
- } else {
133
- error = new Error ( 'Missing element reference' ) ;
134
- }
135
- throw error ;
136
- }
137
- return Plotly . react ( this . el , {
138
- data : nextProps . data ,
139
- layout : nextProps . layout ,
140
- config : nextProps . config ,
141
- frames : nextProps . frames ,
142
- } ) ;
143
- } )
144
- . then ( ( ) => this . syncEventHandlers ( nextProps ) )
145
- . then ( ( ) => this . syncWindowResize ( nextProps ) )
146
- . then ( ( ) => this . figureCallback ( nextProps . onUpdate ) )
147
- . catch ( err => {
148
- if ( err . reason === 'unmounting' ) {
149
- return ;
150
- }
151
- console . error ( 'Error while plotting:' , err ) ; // eslint-disable-line no-console
152
- if ( this . props . onError ) {
153
- this . props . onError ( err ) ;
154
- }
155
- } ) ;
130
+ this . updatePlotly ( false , this . props . onUpdate , false ) ;
156
131
}
157
132
158
133
componentWillUnmount ( ) {
@@ -175,19 +150,19 @@ export default function plotComponentFactory(Plotly) {
175
150
return ;
176
151
}
177
152
178
- for ( let i = 0 ; i < updateEvents . length ; i ++ ) {
179
- this . el . on ( updateEvents [ i ] , this . handleUpdate ) ;
180
- }
153
+ updateEvents . forEach ( updateEvent => {
154
+ this . el . on ( updateEvent , this . handleUpdate ) ;
155
+ } ) ;
181
156
}
182
157
183
158
removeUpdateEvents ( ) {
184
159
if ( ! this . el || ! this . el . removeListener ) {
185
160
return ;
186
161
}
187
162
188
- for ( let i = 0 ; i < updateEvents . length ; i ++ ) {
189
- this . el . removeListener ( updateEvents [ i ] , this . handleUpdate ) ;
190
- }
163
+ updateEvents . forEach ( updateEvent => {
164
+ this . el . removeListener ( updateEvent , this . handleUpdate ) ;
165
+ } ) ;
191
166
}
192
167
193
168
handleUpdate ( ) {
@@ -203,21 +178,18 @@ export default function plotComponentFactory(Plotly) {
203
178
}
204
179
}
205
180
206
- syncWindowResize ( propsIn , invoke ) {
207
- const props = propsIn || this . props ;
181
+ syncWindowResize ( invoke ) {
208
182
if ( ! isBrowser ) {
209
183
return ;
210
184
}
211
185
212
- if ( props . useResizeHandler && ! this . resizeHandler ) {
213
- this . resizeHandler = ( ) => {
214
- return Plotly . Plots . resize ( this . el ) ;
215
- } ;
186
+ if ( this . props . useResizeHandler && ! this . resizeHandler ) {
187
+ this . resizeHandler = ( ) => Plotly . Plots . resize ( this . el ) ;
216
188
window . addEventListener ( 'resize' , this . resizeHandler ) ;
217
189
if ( invoke ) {
218
190
this . resizeHandler ( ) ;
219
191
}
220
- } else if ( ! props . useResizeHandler && this . resizeHandler ) {
192
+ } else if ( ! this . props . useResizeHandler && this . resizeHandler ) {
221
193
window . removeEventListener ( 'resize' , this . resizeHandler ) ;
222
194
this . resizeHandler = null ;
223
195
}
@@ -232,13 +204,9 @@ export default function plotComponentFactory(Plotly) {
232
204
}
233
205
234
206
// Attach and remove event handlers as they're added or removed from props:
235
- syncEventHandlers ( propsIn ) {
236
- // 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 ] ;
241
- const prop = props [ 'on' + eventName ] ;
207
+ syncEventHandlers ( ) {
208
+ eventNames . forEach ( eventName => {
209
+ const prop = this . props [ 'on' + eventName ] ;
242
210
const hasHandler = Boolean ( this . handlers [ eventName ] ) ;
243
211
244
212
if ( prop && ! hasHandler ) {
@@ -249,7 +217,7 @@ export default function plotComponentFactory(Plotly) {
249
217
this . el . removeListener ( 'plotly_' + eventName . toLowerCase ( ) , this . handlers [ eventName ] ) ;
250
218
delete this . handlers [ eventName ] ;
251
219
}
252
- }
220
+ } ) ;
253
221
}
254
222
255
223
render ( ) {
@@ -281,9 +249,9 @@ export default function plotComponentFactory(Plotly) {
281
249
divId : PropTypes . string ,
282
250
} ;
283
251
284
- for ( let i = 0 ; i < eventNames . length ; i ++ ) {
285
- PlotlyComponent . propTypes [ 'on' + eventNames [ i ] ] = PropTypes . func ;
286
- }
252
+ eventNames . forEach ( eventName => {
253
+ PlotlyComponent . propTypes [ 'on' + eventName ] = PropTypes . func ;
254
+ } ) ;
287
255
288
256
PlotlyComponent . defaultProps = {
289
257
debug : false ,
0 commit comments