@@ -100,10 +100,6 @@ export function useEchart({
100
100
[ setEchartOptions ] ,
101
101
) ;
102
102
103
- const resizeEchartInstance = withInstanceCheck ( ( ) => {
104
- echartsInstance ?. resize ( ) ;
105
- } ) ;
106
-
107
103
const addToGroup : EChartsInstance [ 'addToGroup' ] = useCallback (
108
104
withInstanceCheck ( ( groupId : string ) => {
109
105
// echartsCoreRef.current should exist if instance does, but checking for extra safety
@@ -260,23 +256,31 @@ export function useEchart({
260
256
} ) ;
261
257
262
258
/**
259
+ * CHART INITIALIZATION ---------------------
263
260
* Sets up the echart instance on initial render or if the container changes.
264
261
* Additionally, disposes of echart instance and cleans up handlers on unmount.
265
262
*/
266
263
useEffect ( ( ) => {
267
264
setError ( null ) ;
268
265
266
+ let resizeCallback : ( ) => void ;
267
+
269
268
initializeEcharts ( )
270
269
. then ( echartsCore => {
271
270
echartsCoreRef . current = echartsCore ;
272
271
273
272
if ( container ) {
274
273
// Init an echart instance
275
274
const newChart = echartsCoreRef . current . init ( container ) ;
275
+
276
276
// Set the initial options on the instance
277
277
newChart . setOption ( options ) ;
278
+
278
279
// Resize chart when window resizes because echarts don't be default
279
- window . addEventListener ( 'resize' , resizeEchartInstance ) ;
280
+ resizeCallback = ( ) => {
281
+ newChart . resize ( ) ;
282
+ } ;
283
+ window . addEventListener ( 'resize' , resizeCallback ) ;
280
284
281
285
setEchartsInstance ( newChart ) ;
282
286
setReady ( true ) ;
@@ -293,7 +297,7 @@ export function useEchart({
293
297
} ) ;
294
298
295
299
return ( ) => {
296
- window . removeEventListener ( 'resize' , resizeEchartInstance ) ;
300
+ window . removeEventListener ( 'resize' , resizeCallback ) ;
297
301
activeHandlers . current . clear ( ) ;
298
302
299
303
if ( echartsInstance ) {
@@ -303,6 +307,7 @@ export function useEchart({
303
307
} , [ container ] ) ;
304
308
305
309
/**
310
+ * SETTING THEME ---------------------
306
311
* Sets the theme when the instance is created or the theme changes.
307
312
* This is not actually necessary on initial render because the theme
308
313
* is also set on the default options. This is primarily necessary
0 commit comments