0

I am getting the Unexpected Token on my ReactJS application. But I believe the syntax is correct.

import React, { Component } from 'react';

class Auth extends Component {
    constructor(){
        super()
        this.state={
            authStatus: "Sign In",
            isAuthenticated: false
        }
    }
    AuthMe = () =>{
        console.log("Working")
    }
    render() {
        return (
            <button type="button" onClick={this.AuthMe}>{this.state.authStatus}</button>
        );
    }
}

export default Auth;

ERROR in ./src/components/Auth/index.js Module build failed: SyntaxError: Unexpected token (11:11)

> 11 |     AuthMe = () =>{
     |            ^
  12 |         console.log("Working")
  13 |     }

What am I missing?

UPDATE

Here's my webpack.config.js

const path = require('path');
module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/',
  },
  module: {
    rules: [{
      test: /\.jsx?$/,
      exclude: [
        path.resolve(__dirname, 'node_modules'),
      ],
      loader: 'babel-loader',
    }]
  },
  resolve: {
    extensions: ['.json', '.js', '.jsx', '.css']
  },
  devtool: 'source-map',
  devServer: {
    historyApiFallback: true
  }
};

.babelrc

{
    "presets": ["es2015","react","stage-0"],
    "plugins": ["transform-decorators-legacy"]
}
9
  • can you show how you configure your webpack Commented May 9, 2017 at 10:40
  • Sure gimme a second Commented May 9, 2017 at 10:41
  • @SaurabhSharma .babelrc ? Commented May 9, 2017 at 10:45
  • @ShubhamKhatri Updated Commented May 9, 2017 at 10:46
  • 1
    So your code is definitely valid, compiles fine in the Babel REPL. Looks like something to do with your webpack config. - Can you show me your file structure? package.json as well? Commented May 9, 2017 at 11:40

3 Answers 3

0

Try:

class Auth extends Component {
    constructor(){
        super()
        this.state={
            authStatus: "Sign In",
            isAuthenticated: false
        }
    }
    AuthMe() {
        console.log("Working")
    }
    render() {
        return (
            <button type="button" onClick={this.AuthMe}>{this.state.authStatus}</button>
        );
    }
}

NOTE: AuthMe is not just a regular function and not a fat-arrow function, if that matters to you.

EDIT (after comment)

Using fat-arrow functions as class functions is not standard ES6 syntax (the proposal is stage-2 at the moment), so you will need an additional babel plugin to add the feature.

See this answer for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

I am going to setState in the AuthMe() that's why fat arrow to avoid bind
simply add the bind. I don't understand why people are so against adding a binding line, it's MUCH better to add a binding line than to rely on some stage-2 proposals.
0

I answered this question before here: Arrow Function syntax not working with webpack?

TLDR Is that that fat arrow is not yet in the ES201whatever standard. You'll need to add an additional babel transform.

npm install --save-dev babel-plugin-transform-class-properties

EDIT: saw your babelrc config above, run the above then change .babelrc to

{
    "presets": ["es2015","react","stage-0"],
    "plugins": ["transform-decorators-legacy", "transform-class-properties"]
}

1 Comment

That comes in stage-0 as well
-2

use var AuthMe or const AuthMe instead of AuthMe

import React, { Component } from 'react';

    class Auth extends React.Component {
        constructor(){
            super()
            this.state={
                authStatus: "Sign In",
                isAuthenticated: false
            }
        }
        var AuthMe = () =>{
            console.log("Working")
        }
        render() {
            return (
                <button type="button" onClick={this.AuthMe}>{this.state.authStatus}</button>
            );
        }
    }

    export default Auth;

1 Comment

I don't think that will make any difference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.