-1

I am trying to create a component using ES2016 syntax as:

export default class {
  static defaultProps = {
    color: '#cc7f29',
  }
}

I believe static defaultProps = {} syntax is part of ES2016 so I loaded ES2016 preset into Babel. I have installed babel-preset-es2016 already.

This gives error in compilation:

Module build failed: SyntaxError: Unexpected token (10:22)

   8 | export default class extends React.Component {
   9 | 
> 10 |   static defaultProps = {
     |                       ^
  11 |     color: '#cc7f29',
  12 |     theme: 'light',
  13 |   }

BabelLoaderError: SyntaxError: Unexpected token (10:22)

   8 | export default class extends React.Component {
   9 | 
> 10 |   static defaultProps = {
     |                       ^
  11 |     color: '#cc7f29',
  12 |     theme: 'light',
  13 |   }

What am I doing wrong?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Swapnil Devesh
  • 224
  • 8
  • 19
  • If you read the [tag:ecmascript-7] description, then you can see that public class fields are not part of that revision. – Felix Kling Dec 10 '16 at 21:05

1 Answers1

1

Class properties are ECMAScript stage 2 proposal, so you need to include stage 2 preset.

Using stage 0 preset works too, because it includes all previous stage presets.

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177