【问题标题】:ReactJS Syntax Error: Unexpected TokenReactJS 语法错误:意外的令牌
【发布时间】:2017-10-07 15:05:34
【问题描述】:

我在我的 ReactJS 应用程序上获得了 Unexpected Token。但我相信语法是正确的。

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;

./src/components/Auth/index.js 中的错误 模块构建失败:SyntaxError: Unexpected token (11:11)

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

我错过了什么?

更新

这是我的 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"]
}

【问题讨论】:

  • 你能展示你如何配置你的 webpack
  • 请稍等
  • @SaurabhSharma .babelrc ?
  • @ShubhamKhatri 更新
  • 所以你的代码肯定是有效的,在Babel REPL中编译得很好。看起来与您的 webpack 配置有关。 - 你能告诉我你的文件结构吗? package.json 也是?

标签: javascript reactjs ecmascript-6


【解决方案1】:

试试:

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>
        );
    }
}

注意:AuthMe 不仅仅是一个常规函数,也不是一个粗箭头函数,如果这对你很重要的话。

编辑(评论后)

使用胖箭头函数作为类函数不是标准的 ES6 语法(the proposal 目前是第 2 阶段),因此您需要一个 additional babel plugin 来添加该功能。

更多详情请见this answer

【讨论】:

  • 我要去setStateAuthMe() 这就是为什么胖箭头要避免绑定
  • 只需添加绑定。我不明白为什么人们如此反对添加绑定线,添加绑定线比依赖某些第 2 阶段的提案要好得多。
【解决方案2】:

我之前在这里回答过这个问题:Arrow Function syntax not working with webpack?

TLDR 是那个胖箭头还没有在 ES201whatever 标准中。您需要添加一个额外的 babel 转换。

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

编辑:看到上面的 babelrc 配置,运行上面然后将 .babelrc 更改为

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

【讨论】:

  • 这也出现在第 0 阶段
【解决方案3】:

使用 var AuthMe 或 const AuthMe 代替 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;

【讨论】:

  • 我认为这不会有什么不同
猜你喜欢
  • 1970-01-01
  • 2015-03-21
  • 2018-09-10
  • 2014-01-21
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
  • 2019-10-28
  • 2019-01-17
相关资源
最近更新 更多