-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactD3.js
More file actions
69 lines (56 loc) · 1.58 KB
/
ReactD3.js
File metadata and controls
69 lines (56 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React, {
useCallback,
useEffect,
useRef,
useState,
} from 'react';
import * as d3 from "d3";
export const useReactD3 = ({data, render, initProps, resizeRerenderTimeout=1000}) => {
const d3Container = useRef(null)
const [myTimeout, setMyTimeout] = useState(null)
const d3Props = useRef(initProps)
const reRender = useCallback(
() => {
const containerElement = d3Container.current || null
if(containerElement){
const svg = d3.select(containerElement)
const svgBounds = containerElement.getBoundingClientRect()
render(svg, svgBounds, data, d3Props)
}
},
[data, render]
)
useEffect(
() => {
reRender()
},
[reRender]
)
useEffect(
() => {
const handleResize = () => {
const timer = setTimeout(() => {
reRender()
}, resizeRerenderTimeout);
setMyTimeout(timer)
}
window.addEventListener('resize', handleResize)
return _ => {
window.removeEventListener('resize', handleResize)
if(myTimeout){
clearTimeout(myTimeout)
}
}
},
[myTimeout, reRender, resizeRerenderTimeout]
)
return {
d3Container,
d3Props,
}
}
export const ReactD3 = ({d3Container, height}) => {
return <div style={{height}} >
<svg ref={d3Container} width="100%" height="100%" />
</div>
}