Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6: constructor can't declare as arrow function? [duplicate]

Here is my React.js component definition:

class ExampleComponent1 extends React.Component {
    constructor() {
        super();
    }
}

When webpack this with babel loader, everything would be fine. But then I change the constructor declare to arrow function:

class ExampleComponent1 extends React.Component {
    constructor = () => {
        super();
    }
}

webpack build failed:

Module build failed: SyntaxError:....: 'super' outside of function or class (8:4)

I don't know why this happened, constructor can't declare as arrow function?

like image 958
hh54188 Avatar asked Mar 07 '26 06:03

hh54188


1 Answers

In ES6, functions can be maked using arrow but only if they are procedural or lambda functions !

Into class declarations, you are only allowed to use the standard ES6 syntax :

class ExampleComponent1 extends React.Component {
    constructor() {
        super();
    }
}
like image 105
ClementNerma Avatar answered Mar 09 '26 19:03

ClementNerma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!