Babel-loader: how to use arrow function in babel 6.x

Created on 2 Nov 2015  路  3Comments  路  Source: babel/babel-loader

this is my babelrc config

{
  "presets": [
    "react",
    "es2015"
  ],
  "plugins": [
    "transform-es2015-arrow-functions"
  ]
}

and my component code:

import React from 'react';
import { render } from 'react-dom';

class App extends React.Component {
    constructor(props) {
        super(props);
    }
    _handleClick = (e) => {

    }
    render() {
        return (
            <div>App</div>
        );
    }
}

render(
    <App />,
    document.getElementById('app')
);

but it throw an error when webpack or babel main.js -p out.js

SyntaxError: main.js: Unexpected token (9:17)
   7 |         super(props);
   8 |     }
>  9 |     _handleClick = (e) => {
     |                  ^
  10 |         
  11 |     }
  12 |     render() {
question

Most helpful comment

{
  "presets": [
    "react",
    "stage-1"
  ]
}

solved

All 3 comments

You need to enable class properties. (i.e. use the appropriate plugin for that under Babel 6)

@mathieumg thanks your answer. can you give some demo code like my babelrc?

{
  "presets": [
    "react",
    "stage-1"
  ]
}

solved

Was this page helpful?
0 / 5 - 0 ratings