Skip to content Skip to sidebar Skip to footer

Cannot Store Arrow Function In Let/var In React Component

I have a component in react defined by extends React.Component. Inside it, along with the render method, is: constructor(props, context) { super(props, context); this.stat

Solution 1:

Read babel documentation about classes. using let or const is not part of ES6 possible prefix for classes definition

Solution 2:

The ECMA 2015 spec seems to only allow methods and static methods in the body of a class definition.

So, in this case, assignment statements are not allowed, hence the Unexpected token error from babel.

Solution 3:

You're trying to use experimental ES7 features and likely only have ES6 enabled (also you cannot prefix let or const in a class). If you're using babel6 it should be simple to switch to the correct plugin. See this answer from basically the same question: How to use ES6 arrow in class methods?

Solution 4:

You seem to be making use of the class properties declartion proposal. The proposed syntax is:

classClassWithoutInits {
  myProp;
}

classClassWithInits {
  myProp = 42;
}

As you can see, no let or const or var. let empty = () => {}; doesn't work because it is not valid syntax, just like the error message tells you.

Post a Comment for "Cannot Store Arrow Function In Let/var In React Component"