react-native-canvas icon indicating copy to clipboard operation
react-native-canvas copied to clipboard

removeEventListener()

Open myselfyy opened this issue 7 years ago • 11 comments

Hello, iddan, when I unMount my component, I get the error : "ExceptionsManager.js:65 Error: Image must be initialized with a Canvas instance".I think we should remove the Image load listener when I unmount the component in the ComponentWillUnMount function, but I dot't know how to do it,please give me a favor,thanks a lot!

myselfyy avatar Nov 01 '18 03:11 myselfyy

Hey! Can you share your code so I could give an advice?

iddan avatar Nov 02 '18 18:11 iddan

Yes! The code is :

componentWillUnmount() {
       this.image.removeEventListener('load', this.renderImage);
       // window.removeEventListener('load', this.handleImageRect);
   }
   handleImageRect = (canvas) => {
       this.image = new CanvasImage(canvas);
       canvas.width = 312;
       canvas.height = 413;
       const context = canvas.getContext('2d');
       const that = this;
       this.image.src = 'https://image.freepik.com/free-vector/unicorn-background-design_1324-79.jpg';
       this.image.addEventListener('load', () => {
           context.drawImage(this.image, 0, 0, 312, 413);
           context.fillStyle = 'black';
           context.font = 13 + 'px PingFangSC-Regular';
           context.fillText('二零一八年 九月', 22, 30);
           context.font = 62 + 'px Didot';
           context.fillText('28', 22, 90);
           context.font = 62 + 'px Didot';
           context.fillText('一', 22, 150);
           context.font = 13 + 'px PingFangSC-Regular';
           context.fillText('我的金额:320', 22, 180);
           context.font = 13 + 'px PingFangSC-Regular';
           context.fillText('这是一条签语,预计可以放下二十八个汉字', 22, 300);
           context.font = 13 + 'px PingFangSC-Regular';
           context.fillText('---作者', 250, 330);
           const that = this;
           console.log('888', this.image);
           canvas.toDataURL('image/png').then((testImg) => {
               this.setState({
                   testImg: testImg
               })
           })
       });
   }
   render() {
       return <Canvas ref={this.handleImageRect} />
   }

11 22

when click the topLeft button , componentUnmounted, but the 'load' listener is always presence

myselfyy avatar Nov 05 '18 03:11 myselfyy

It seems like removeEventListener() implementation is missing. It should be added here: https://github.com/iddan/react-native-canvas/blob/983aa21d6eef86c108cd3b15b87ca373ffe221f4/src/webview-binders.js#L101

iddan avatar Nov 05 '18 09:11 iddan

OK, thanks a lot! When do you update the removeEventListener function?

myselfyy avatar Nov 05 '18 11:11 myselfyy

I'm not available to do this currently but very willing to guide for a PR.

iddan avatar Nov 06 '18 12:11 iddan

OK! Tanks a lot!

myselfyy avatar Nov 09 '18 07:11 myselfyy

I had the same problem.

SiJieMa avatar Jan 08 '19 07:01 SiJieMa

Hi iddan!You write so cool code ,but I can't understand it quickly. My question is: Why is the component uninstalled and canvas. JS still running? I hope you can solve this removeEventListene() bug.THANKS!

SiJieMa avatar Jan 09 '19 04:01 SiJieMa

Hey SiJieMa, thank you for using React Native Canvas. Currently, the implementation misses a definition for the removeEventListener(). To implement it you should mimic the implementation of addEventListener(). As said before: I'm not available to do this currently but very willing to guide for a PR.

iddan avatar Jan 09 '19 18:01 iddan

Okay, I'll try to solve this problem these days.

SiJieMa avatar Jan 10 '19 01:01 SiJieMa

@myselfyy, @SiJieMa, How did you solve this problem?

sergey-kompaniets avatar Jul 03 '19 12:07 sergey-kompaniets