0

I'm getting the error:'TypeError: this.createCanvas is not a function'.

I don't understand why. I've bound it in the constructor.

import React, { Component } from 'react';

class ImageEditor extends Component {

constructor() {
    super();
    this.createCanvas = this.createCanvas.bind(this);
    this.getImageDimensions = this.getImageDimensions.bind(this);
}

componentDidMount() {
    const imageurl = "https://storage.googleapis.com/" + this.props.location.state.imageuri;
    this.getImageDimensions(imageurl);
}

getImageDimensions(url) {
    var img = new Image();

    img.onload = function() {
        this.createCanvas(this.width, this.height);
    }

    img.src = url;
}

createCanvas(width, height) {
    let mycanvas = document.createElement("canvas");
    mycanvas.width = width;
    mycanvas.height = height;
    mycanvas.id = "mycanvas";
    document.body.appendChild(mycanvas);
}

render() {
    return (
        <h1>{this.imageurl}</h1>
    )
}

}

export default ImageEditor;

2
  • img.onload = () => {....} Commented Jul 1, 2018 at 10:56
  • createCanvas function doesn't use instance at all. Why do you make it to be a method? Commented Jul 1, 2018 at 11:05

1 Answer 1

0

Your img.onload has to be given an arrow function as well, or this will not be what you want. You can read more about why this is the case here.

img.onload = () => {
    this.createCanvas(img.width, img.height);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.