Parcel: Unexpected error with state={} in react class component

Created on 12 Jan 2018  路  5Comments  路  Source: parcel-bundler/parcel

Hey there,

I'm getting the following error:

/Users/tnrich/Sites/treeview/Comp.js:10:7: Unexpected token (10:7)                     elDigestEnzymes });
> 10 |   state={
     |        ^

from the following piece of code

export default class Ladder extends React.Component {
  state={
    highlightedFragment: undefined 
  }

The following code works and does the same thing, but I'd like to be able to do the top version.

export default class Ladder extends React.Component {
  // constructor(props) {
  //   super(props);
  //   this.state = { highlightedFragment: undefined };
  // }

Does anyone know which babel transform I need to enable to get this to work? Thanks!

Most helpful comment

All 5 comments

I think you should use super() in constructor() {} before you use this when u extend a class.
In react docs, it's

 constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

@devongovett thanks man!!! Problem resolved!!!

@devongovett thx for the link!

However the main problem for me was my webpack config. Just deleted Babel options from _webpack.config.js_ and used only _.babelrc_ configuration instead.

webpack.config.js before

  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        options: {presets: ['env', 'react']}  //bad line here
      },
...

webpack.config.js after

  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader'
      },
...

.babelrc

{
  "presets": [
    "env",
    "react"
  ],
  "plugins": [
    "transform-class-properties"
  ]
}

I have the same problem but with the Aribnb linter...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcfarljw picture mcfarljw  路  46Comments

garrydzeng picture garrydzeng  路  95Comments

mjrjoh picture mjrjoh  路  40Comments

shiloa picture shiloa  路  39Comments

v0idifier picture v0idifier  路  51Comments