Skip to content
This repository was archived by the owner on Aug 1, 2022. It is now read-only.

Commit 861b743

Browse files
laeckervembiem
authored andcommitted
Properly unobserve on componentWillUnmount (#13)
In my scenario I unmount the CanvasDraw when `window.innerWidth < 992` which resulted in the following error: ``` index.js:445 Uncaught DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0. at _default._this.saveLine (http://localhost:3000/static/js/22.chunk.js:533:25) at http://localhost:3000/static/js/22.chunk.js:311:17 ``` Those are lines `408: this.ctx.drawing.drawImage(this.canvas.temp, 0, 0, width, height);` and `241: this.saveLine({ brushColor, brushRadius });` I figured out that `saveLine` was called from `simulateDrawingLines`, from the `loadSaveData` from the `handleCanvasResize`. Which fired at the same moment, when the component actually got unmounted. The simplest solution is to properly unobserve the `ResizeObserver`, which this PR does.
1 parent bb7db6e commit 861b743

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ export default class extends PureComponent {
9999
});
100100
this.chainLength = this.props.lazyRadius * window.devicePixelRatio;
101101

102-
const observeCanvas = new ResizeObserver((entries, observer) =>
102+
this.canvasObserver = new ResizeObserver((entries, observer) =>
103103
this.handleCanvasResize(entries, observer)
104104
);
105-
observeCanvas.observe(this.canvasContainer);
105+
this.canvasObserver.observe(this.canvasContainer);
106106

107107
this.drawImage();
108108
this.loop();
@@ -146,6 +146,10 @@ export default class extends PureComponent {
146146
}
147147
}
148148

149+
componentWillUnmount = () => {
150+
this.canvasObserver.unobserve(this.canvasContainer);
151+
};
152+
149153
drawImage = () => {
150154
if (!this.props.imgSrc) return;
151155

0 commit comments

Comments
 (0)