Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babel unexpected token compiling react component

I'm trying to compile the following code using babel but it's giving me an "unexpected token" on the character "=" on the following line:

state = {};

Babel version is: 6.24.1 (babel-core 6.25.0)

The code:

import {Button, Menu} from 'semantic-ui-react';

class AppBar extends Component {
    state = {};

    handleItemClick = (e, { name }) => this.setState({ activeItem: name });

    render() {
        const { activeItem } = this.state;

        return (
            <Menu>
                <Menu.Item
                    name='dashboard'
                    active={activeItem === 'dashboard'}
                    onClick={this.handleItemClick}
                >
                    Dashboard
                </Menu.Item>

                <Menu.Item
                    name='contacts'
                    active={activeItem === 'contacts'}
                    onClick={this.handleItemClick}
                >
                    Contact Lists
                </Menu.Item>

                <Menu.Item
                    name='messages'
                    active={activeItem === 'messages'}
                    onClick={this.handleItemClick}
                >
                    Messages
                </Menu.Item>
            </Menu>
        )
    }
}

ReactDOM.render(
  <AppBar/>,
  document.getElementById('root')
);

I'm using the es2015 and react presets.

What is the problem? Thanks.

like image 478
SrgHartman Avatar asked Aug 14 '26 14:08

SrgHartman


2 Answers

Add package babel-plugin-transform-class-properties to your project.

like image 72
Knut Herrmann Avatar answered Aug 17 '26 04:08

Knut Herrmann


I'm currently using these babel modules (in the package.json) for our React app:

"babel-cli": "~6.24.1",
"babel-core": "~6.25.0",
"babel-eslint": "~7.2.3",
"babel-loader": "~7.1.0",
"babel-plugin-add-module-exports": "~0.2.1",
"babel-plugin-istanbul": "^4.1.3",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "~6.24.1",
"babel-preset-es2015": "~6.24.1",
"babel-preset-react": "~6.24.1",
"babel-preset-stage-3": "^6.24.1",
"babel-register": "~6.24.1",
"babel-runtime": "~6.23.0",

And I've the following .bablerc on the root folder:

{
  "presets": [
    "es2015", "react", "stage-3"
  ],
  "plugins": ["transform-class-properties", "add-module-exports","syntax-dynamic-import"],
  "env": {
    "development": {
      "plugins": ["react-hot-loader/babel"]
    },
    "coverage": {
      "plugins": ["istanbul"]
    }
  }
}

And I define state using the same code

export class AttributeModelListContainer extends React.Component {

  static propTypes = {
    // from router
    params: PropTypes.shape({
      project: PropTypes.string,
    }),
    // from mapStateToProps
    [...]
  }

  state = {
    isLoading: true,
  }
}
like image 38
Ser Avatar answered Aug 17 '26 04:08

Ser



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!