1
1
import React , {
2
- useCallback , useRef ,
2
+ useEffect , useRef ,
3
3
} from 'react' ;
4
4
import echarts from '../../echarts' ;
5
5
@@ -87,7 +87,7 @@ function initChart(inst, opt = {}) {
87
87
emphasis : {
88
88
show : false
89
89
}
90
- } ,
90
+ } ,
91
91
data : mydata . map ( item => ( {
92
92
name : replaceProvinceName ( item . name ) ,
93
93
value : item . value ,
@@ -104,25 +104,35 @@ inst.setOption(optionMap, opt);
104
104
105
105
function DataChart ( ) {
106
106
const myChart = useRef ( null ) ;
107
- const chartDOMRef = useCallback ( ( el ) => {
108
- if ( el ) {
109
- if ( ! myChart . current ) {
110
- myChart . current = echarts . init ( el ) ;
111
- }
112
- initChart ( myChart . current , {
113
- notMerge : true ,
114
- } ) ;
115
- } else {
116
- if ( ! myChart . current ) {
117
- return ;
118
- }
107
+ useEffect ( ( ) => {
108
+ myChart . current = echarts . init ( myChart . current ) ;
109
+ initChart ( myChart . current , {
110
+ notMerge : true ,
111
+ } ) ;
112
+ return ( ) => {
113
+ // console.log('销毁时');
119
114
myChart . current . dispose ( ) ;
120
- }
115
+ } ;
121
116
} , [ ] ) ;
117
+ // const chartDOMRef = useCallback((el) => {
118
+ // if (el) {
119
+ // if (!myChart.current) {
120
+ // myChart.current = echarts.init(el);
121
+ // }
122
+ // initChart(myChart.current, {
123
+ // notMerge: true,
124
+ // });
125
+ // } else {
126
+ // if (!myChart.current) {
127
+ // return;
128
+ // }
129
+ // myChart.current.dispose();
130
+ // }
131
+ // }, []);
122
132
123
133
return (
124
134
< >
125
- < div ref = { chartDOMRef } style = { { height : 300 , backgroundColor : '#dedede' } } />
135
+ < div ref = { myChart } style = { { height : 300 , backgroundColor : '#dedede' } } />
126
136
</ >
127
137
) ;
128
138
}
0 commit comments