Skip to content

Commit cd078fe

Browse files
author
lindelof
committed
fixed read property 'clientHeight' of null
fixed read property 'clientHeight' of null
1 parent 57b41d4 commit cd078fe

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "particles-bg",
3-
"version": "2.4.7",
3+
"version": "2.5.0",
44
"description": "a particles background for react",
55
"author": "lindelof",
66
"license": "MIT",

src/particles/Canvas.jsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default class Canvas extends React.Component {
55
constructor(props) {
66
super(props);
77

8+
this._id = 0;
89
this.size = { width: 0, height: 0 };
910
this.canvasRef = React.createRef();
1011
}
@@ -33,17 +34,24 @@ export default class Canvas extends React.Component {
3334
}
3435

3536
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+
}
4144
}
4245
}, 1000 / 10);
4346
}
4447

4548
componentWillUnmount() {
46-
window.removeEventListener("resize", this.resize);
49+
try{
50+
window.removeEventListener("resize", this.resize);
51+
clearInterval(this._id);
52+
}catch(e){
53+
54+
}
4755
}
4856

4957
resize() {
@@ -55,11 +63,11 @@ export default class Canvas extends React.Component {
5563
setCanvasSize(canvas) {
5664
const width = this.canvasRef.current.clientWidth;
5765
const height = this.canvasRef.current.clientHeight;
66+
5867
this.size.width = width;
5968
this.size.height = height;
6069
canvas.width = width;
6170
canvas.height = height;
62-
6371
return { width, height };
6472
}
6573

0 commit comments

Comments
 (0)