Skip to content

Commit b95f6ec

Browse files
committed
fix: change state Ctor error
1 parent 03df02d commit b95f6ec

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

demo/index.tsx

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import ReactDOM from 'react-dom';
2-
import React from 'react';
3-
import { Line } from '@antv/g2plot';
4-
2+
import React, { useState, useEffect } from 'react';
3+
import { Line, Column } from '@antv/g2plot';
54
import ReactG2Plot from '../src';
65

7-
const data = [
6+
const Data = [
87
{ year: '1991', value: 3 },
98
{ year: '1992', value: 4 },
109
{ year: '1993', value: 3.5 },
@@ -16,8 +15,7 @@ const data = [
1615
{ year: '1999', value: 13 },
1716
];
1817

19-
// line chart config
20-
const config = {
18+
const LineConfig = {
2119
title: {
2220
visible: true,
2321
text: '曲线折线图',
@@ -28,17 +26,55 @@ const config = {
2826
},
2927
padding: 'auto',
3028
forceFit: true,
31-
data,
29+
data: Data,
3230
xField: 'year',
3331
yField: 'value',
3432
smooth: true,
3533
};
3634

37-
function ref(plot) {
38-
console.log('G2Plot instance Ref', plot);
35+
const ColumnConfig = {
36+
title: {
37+
visible: true,
38+
text: '柱形图',
39+
},
40+
description: {
41+
visible: true,
42+
text: '使用柱形图展示每一年的对比情况。',
43+
},
44+
padding: 'auto',
45+
forceFit: true,
46+
data: Data,
47+
xField: 'year',
48+
yField: 'value',
49+
};
50+
51+
function Entry() {
52+
const [isLine, setIsLine] = useState<boolean>(true);
53+
54+
function ref(plot) {
55+
console.log('G2Plot instance Ref', plot);
56+
}
57+
58+
function getCtor() {
59+
return isLine ? Line : Column;
60+
}
61+
62+
function getConfig() {
63+
return isLine ? LineConfig : ColumnConfig;
64+
}
65+
66+
useEffect(() => {
67+
const id = setInterval(() => {
68+
// 切换
69+
setIsLine(!isLine);
70+
}, 3000);
71+
72+
return () => {
73+
clearInterval(id);
74+
};
75+
}, [isLine]);
76+
77+
return <ReactG2Plot className="g2plot-for-react" Ctor={getCtor()} config={getConfig()} ref={ref} />;
3978
}
4079

41-
ReactDOM.render(
42-
<ReactG2Plot className="g2plot-for-react" Ctor={Line} config={config} ref={ref} />,
43-
document.getElementById('root-container'),
44-
);
80+
ReactDOM.render(<Entry />, document.getElementById('root-container'));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-g2plot",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Unofficial react component wrapper for @antv/g2plot.",
55
"main": "lib/index.js",
66
"module": "esm/index.js",

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default forwardRef((props: ReactG2PlotProps, ref: any) => {
3434
function newPlot() {
3535
if (plotRef.current) {
3636
plotRef.current.destroy();
37+
plotRef.current = undefined;
3738
}
3839

3940
plotRef.current = new Ctor($dom.current, config);
@@ -49,6 +50,7 @@ export default forwardRef((props: ReactG2PlotProps, ref: any) => {
4950
return () => {
5051
if (plotRef.current) {
5152
plotRef.current.destroy();
53+
plotRef.current = undefined;
5254
}
5355
};
5456
}, [Ctor, config]);

0 commit comments

Comments
 (0)