Skip to content

Commit 03df02d

Browse files
committed
refactor: use hooks
1 parent 2284e05 commit 03df02d

File tree

7 files changed

+43
-135
lines changed

7 files changed

+43
-135
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,3 @@ jobs:
2727
- name: npm run build
2828
run: |
2929
npm run build
30-
- name: gh-pages
31-
uses: ad-m/github-push-action@master
32-
with:
33-
github_token: ${{ secrets.GITHUB_TOKEN }}
34-
directory: public
35-
branch: gh-pages
36-
force: true

.github/workflows/rebase.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![npm](https://img.shields.io/npm/v/react-g2plot.svg)](https://www.npmjs.com/package/react-g2plot)
88
[![Build Status](https://github.com/hustcc/react-g2plot/workflows/build/badge.svg)](https://github.com/hustcc/react-g2plot/actions)
99
[![npm](https://img.shields.io/npm/dm/react-g2plot.svg)](https://www.npmjs.com/package/react-g2plot)
10-
[![react supported](https://img.shields.io/badge/React-%5E0.14.0%20%7C%7C%20%5E15.0.0%20%7C%7C%20%5E16.0.0-blue.svg)](https://github.com/hustcc/react-g2plot)
10+
[![React Supported](https://img.shields.io/badge/React-%3E=16.8.0-blue.svg)](https://github.com/hustcc/react-g2plot)
1111
[![npm](https://img.shields.io/npm/l/react-g2plot.svg)](https://www.npmjs.com/package/react-g2plot)
1212

1313

demo/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ const config = {
3434
smooth: true,
3535
};
3636

37+
function ref(plot) {
38+
console.log('G2Plot instance Ref', plot);
39+
}
40+
3741
ReactDOM.render(
38-
<ReactG2Plot className="g2plot-for-react" Ctor={Line} config={config} />,
42+
<ReactG2Plot className="g2plot-for-react" Ctor={Line} config={config} ref={ref} />,
3943
document.getElementById('root-container'),
4044
);

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-g2plot",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "Unofficial react component wrapper for @antv/g2plot.",
55
"main": "lib/index.js",
66
"module": "esm/index.js",
@@ -16,6 +16,8 @@
1616
"build:cjs": "rimraf ./lib && tsc --module commonjs --outDir lib",
1717
"build:esm": "rimraf ./esm && tsc --module ESNext --outDir esm",
1818
"build:demo": "rimraf ./public && webpack",
19+
"demo": "DEBUG_MODE=1 jest",
20+
"deploy": "gh-pages -d public",
1921
"prepublishOnly": "npm run build"
2022
},
2123
"repository": {
@@ -28,7 +30,7 @@
2830
"react-g2plot",
2931
"antv",
3032
"g2plot",
31-
"ecahrts",
33+
"echarts",
3234
"visualization",
3335
"react-component"
3436
],
@@ -46,6 +48,7 @@
4648
"@types/react-dom": "^16.9.3",
4749
"copy-webpack-plugin": "^5.1.1",
4850
"cross-env": "^5.1.3",
51+
"gh-pages": "^3.0.0",
4952
"husky": "^3.0.9",
5053
"jest": "^24.9.0",
5154
"jest-electron": "^0.1.7",
@@ -62,7 +65,7 @@
6265
},
6366
"peerDependencies": {
6467
"@antv/g2plot": "*",
65-
"react": "^0.14.0 || ^15.0.0 || ^16.0.0"
68+
"react": ">= 16.8.0"
6669
},
6770
"lint-staged": {
6871
"*.{ts,tsx}": [

src/helper.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/index.tsx

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import React, { PureComponent } from 'react';
2-
import { debounce, get, size } from './helper';
1+
import React, { useEffect, useRef, forwardRef } from 'react';
32

43
export type ReactG2PlotProps = {
54
readonly className?: string;
@@ -10,76 +9,49 @@ export type ReactG2PlotProps = {
109
/**
1110
* 一个基本可用的 G2Plot React 组件
1211
*/
13-
export default class extends PureComponent<ReactG2PlotProps> {
14-
/** container */
15-
private $dom: HTMLElement;
16-
/** G2Plot Ctor */
17-
private plot: any;
12+
export default forwardRef((props: ReactG2PlotProps, ref: any) => {
13+
const { className, Ctor, config } = props;
14+
15+
const $dom = useRef(undefined);
16+
const plotRef = useRef(undefined);
1817

1918
/**
20-
* get G2Plot instance
19+
* 同步 forward ref
20+
* @param source
21+
* @param target
2122
*/
22-
public getPlot() {
23-
return this.plot;
24-
}
25-
26-
componentDidMount() {
27-
this.newPlot();
28-
}
29-
30-
componentDidUpdate(prevProps) {
31-
const { Ctor, config } = prevProps;
32-
33-
if (size(get(this.props, ['config', 'data'])) && JSON.stringify(this.props.config) !== JSON.stringify(config)) {
34-
// 只有有数据的时候才去渲染
35-
// 构造函数变化,则重新创建;或者 chart 不存在
36-
if (this.props.Ctor !== Ctor || !this.plot) {
37-
this.newPlot();
38-
} else {
39-
this.updatePlot();
40-
}
23+
function syncRef(source, target) {
24+
if (typeof target === 'function') {
25+
target(source.current);
26+
} else if (target) {
27+
target.current = source.current;
4128
}
4229
}
4330

4431
/**
45-
* 创建 chart
32+
* 创建
4633
*/
47-
private newPlot() {
48-
const { Ctor, config } = this.props;
49-
50-
if (this.plot) {
51-
this.plot.destroy();
52-
this.plot = undefined;
34+
function newPlot() {
35+
if (plotRef.current) {
36+
plotRef.current.destroy();
5337
}
5438

55-
this.plot = new Ctor(this.$dom, config);
56-
57-
this.batchRender();
58-
}
39+
plotRef.current = new Ctor($dom.current, config);
5940

60-
/**
61-
* 更新 chart
62-
*/
63-
private updatePlot() {
64-
if (this.plot) {
65-
this.plot.updateConfig(this.props.config);
41+
syncRef(plotRef, ref);
6642

67-
this.batchRender();
68-
}
43+
plotRef.current.render();
6944
}
7045

71-
/**
72-
* 对 chart.render 加上 debounce
73-
*/
74-
private batchRender = debounce(() => {
75-
if (this.plot) {
76-
this.plot.render();
77-
}
78-
});
46+
useEffect(() => {
47+
newPlot();
7948

80-
render() {
81-
const { className } = this.props;
49+
return () => {
50+
if (plotRef.current) {
51+
plotRef.current.destroy();
52+
}
53+
};
54+
}, [Ctor, config]);
8255

83-
return <div className={className} ref={(e) => (this.$dom = e)} />;
84-
}
85-
}
56+
return <div className={className} ref={$dom} />;
57+
});

0 commit comments

Comments
 (0)