Skip to content

Commit 7b85bf6

Browse files
committed
converted History.tsx from Class to Hooks to simplify lifecycle methods and garbage collection
1 parent 3bfa4f9 commit 7b85bf6

File tree

1 file changed

+31
-55
lines changed

1 file changed

+31
-55
lines changed

src/app/components/History.tsx

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React, { Component, useEffect, useState } from 'react';
22
import * as d3 from 'd3';
33

44
/**
@@ -31,53 +31,41 @@ const filterHooks = (data: any[]) => {
3131
};
3232

3333
interface HistoryProps {
34-
hierarchy:any;
34+
hierarchy: Record<string, unknown>;
3535
}
3636

37-
let root = {};
38-
class History extends Component {
39-
/**
40-
* @method maked3Tree :Creates a new D3 Tree
41-
*/
42-
constructor(props: HistoryProps) {
43-
super(props);
44-
// what React.createRef() is doing.
45-
this.HistoryRef = React.createRef();
46-
this.maked3Tree = this.maked3Tree.bind(this);
47-
this.removed3Tree = this.removed3Tree.bind(this);
48-
this.isRecoil = false;
49-
}
5037

51-
componentDidMount() {
52-
const { hierarchy } = this.props;
53-
root = JSON.parse(JSON.stringify(hierarchy));
54-
this.maked3Tree();
55-
}
38+
/**
39+
* @method maked3Tree :Creates a new D3 Tree
40+
*/
5641

57-
componentDidUpdate() {
58-
const { hierarchy } = this.props;
59-
root = JSON.parse(JSON.stringify(hierarchy));
60-
this.maked3Tree();
61-
}
42+
function History(props) {
43+
let { hierarchy } = props;
44+
let root = JSON.parse(JSON.stringify(hierarchy));
45+
let isRecoil = false;
46+
let HistoryRef = root; //React.createRef(root);
47+
48+
useEffect(() => {
49+
maked3Tree();
50+
}, [root]);
6251

63-
removed3Tree() {
64-
const { current } = this.HistoryRef;
52+
let removed3Tree = function () {
53+
const { current } = HistoryRef;
6554
while (current.hasChildNodes()) {
6655
current.removeChild(current.lastChild);
6756
}
68-
}
57+
};
6958

7059
/**
7160
* @method maked3Tree Creates a new Tree History
7261
* @var
7362
*/
74-
maked3Tree(): void {
75-
this.removed3Tree();
76-
console.log("HIEARARCHY", this.props.hierarchy)
77-
const width : number = 800;
78-
const height : number = 600;
63+
let maked3Tree = function () {
64+
removed3Tree();
65+
const width: number = 800;
66+
const height: number = 600;
7967
const svgContainer = d3
80-
.select(this.HistoryRef.current)
68+
.select(HistoryRef.current)
8169
.append('svg') // svgContainer is now pointing to svg
8270
.attr('width', width)
8371
.attr('height', height);
@@ -93,9 +81,7 @@ class History extends Component {
9381

9482
const tree = d3
9583
.tree()
96-
9784
.nodeSize([width / 10, height / 10])
98-
// .separation(function (a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth; });
9985
.separation(function (a: { parent: object }, b: { parent: object }) {
10086
return a.parent == b.parent ? 2 : 2;
10187
});
@@ -159,17 +145,9 @@ class History extends Component {
159145

160146
if (d.data.stateSnapshot.children[0].name === 'RecoilRoot') {
161147
console.log('enter');
162-
this.isRecoil = true;
148+
isRecoil = true;
163149
}
164-
console.log('isRecoil', this.isRecoil);
165-
166-
console.log('d.data', d.data);
167-
console.log('d.data.stateSnapshot', d.data.stateSnapshot);
168-
console.log(
169-
'd.data.stateSnapshot.children',
170-
d.data.stateSnapshot.children
171-
);
172-
if (!this.isRecoil) {
150+
if (!isRecoil) {
173151
tooltipDiv
174152
.html(filterHooks(d.data.stateSnapshot.children), this)
175153
.style('left', d3.event.pageX - 90 + 'px')
@@ -247,7 +225,7 @@ class History extends Component {
247225
.on('drag', dragged)
248226
.on('end', dragended)
249227
);
250-
228+
251229
function dragstarted() {
252230
d3.select(this).raise();
253231
g.attr('cursor', 'grabbing');
@@ -273,15 +251,13 @@ class History extends Component {
273251
function reinfeldTidierAlgo(x: number, y: number) {
274252
return [(y = +y) * Math.cos((x -= Math.PI / 2)), y * Math.sin(x)];
275253
}
276-
}
254+
};
277255

278-
render() {
279-
return (
280-
<div className="history-d3-container">
281-
<div ref={this.HistoryRef} className="history-d3-div" />
282-
</div>
283-
);
284-
}
256+
return (
257+
<div className="history-d3-container">
258+
<div ref={HistoryRef} className="history-d3-div" />
259+
</div>
260+
);
285261
}
286262

287263
export default History;

0 commit comments

Comments
 (0)