@@ -5,6 +5,7 @@ export default class Canvas extends React.Component {
5
5
constructor ( props ) {
6
6
super ( props ) ;
7
7
8
+ this . _id = 0 ;
8
9
this . size = { width : 0 , height : 0 } ;
9
10
this . canvasRef = React . createRef ( ) ;
10
11
}
@@ -33,17 +34,24 @@ export default class Canvas extends React.Component {
33
34
}
34
35
35
36
heartbeatDetectionCanvasSize ( canvas ) {
36
- setInterval ( ( ) => {
37
- const newHeight = this . canvasRef . current . clientHeight ;
38
- if ( newHeight !== this . size . height ) {
39
- const { width, height } = this . setCanvasSize ( canvas ) ;
40
- this . props . onResize && this . props . onResize ( width , height ) ;
37
+ this . _id = setInterval ( ( ) => {
38
+ if ( this . canvasRef . current ) {
39
+ const newHeight = this . canvasRef . current . clientHeight ;
40
+ if ( newHeight !== this . size . height ) {
41
+ const { width, height } = this . setCanvasSize ( canvas ) ;
42
+ this . props . onResize && this . props . onResize ( width , height ) ;
43
+ }
41
44
}
42
45
} , 1000 / 10 ) ;
43
46
}
44
47
45
48
componentWillUnmount ( ) {
46
- window . removeEventListener ( "resize" , this . resize ) ;
49
+ try {
50
+ window . removeEventListener ( "resize" , this . resize ) ;
51
+ clearInterval ( this . _id ) ;
52
+ } catch ( e ) {
53
+
54
+ }
47
55
}
48
56
49
57
resize ( ) {
@@ -55,11 +63,11 @@ export default class Canvas extends React.Component {
55
63
setCanvasSize ( canvas ) {
56
64
const width = this . canvasRef . current . clientWidth ;
57
65
const height = this . canvasRef . current . clientHeight ;
66
+
58
67
this . size . width = width ;
59
68
this . size . height = height ;
60
69
canvas . width = width ;
61
70
canvas . height = height ;
62
-
63
71
return { width, height } ;
64
72
}
65
73
0 commit comments