0
votes

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"]
}
3
can you show how you configure your webpackShubham Khatri
Sure gimme a secondSaurabh Sharma
@SaurabhSharma .babelrc ?StateLess
@ShubhamKhatri UpdatedSaurabh Sharma
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?Joe Ruello

3 Answers

0
votes

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.

0
votes

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"]
}
-2
votes

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;