reactjs - 带有React和Babel语法错误的Brunch.io

标签 reactjs ecmascript-6 syntax-error babeljs brunch

我第一次使用Brunch.io来构建一个新的React应用程序。
似乎一切正常,但是当我尝试设置状态或使用箭头功能时,出现语法错误。我的猜测是我缺少Babel预设或其他配置。

有人可以帮忙,或者不知道这件丢失的东西可能是什么吗?

控制台错误:

09:27:18 - error: Compiling of app/components/index.js failed. Error: SyntaxError: app/components/index.js: Unexpect
ed token (10:15)
      |      * @return {string}    formatted list of authors
      |      */
   >  |     getAuthors = (arr) => {
      |                ^
      |         if (arr) {
      |             return arr.join('\r\n');
      |         } else {

package.json
{
  "name": "brunch-app",
  "description": "Brunch.io application",
  "private": true,
  "author": "Brunch",
  "version": "0.0.1",
  "repository": "",
  "scripts": {
    "start": "brunch watch --server",
    "build": "brunch build --production"
  },
  "dependencies": {
    "prop-types": "^15.6.0",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-redux": "^5.0.6",
    "react-router-dom": "^4.2.2",
    "redux": "^3.7.2"
  },
  "devDependencies": {
    "auto-reload-brunch": "^2",
    "hmr-brunch": "^0.1",
    "babel-brunch": "~6.0.0",
    "babel-preset-latest": "^6",
    "babel-preset-react": "~6.22",
    "brunch": "^2",
    "clean-css-brunch": "^2",
    "uglify-js-brunch": "^2"
  }
}

brunch-config.js:
exports.files = {
  javascripts: {
    joinTo: 'app.js'
  },
  stylesheets: {joinTo: 'app.css'}
};

exports.plugins = {
  babel: {presets: ['es2015', 'react']}
};

exports.hot = true;

组件/index.js无法编译:
import React from 'react';

class Book extends React.Component {

    state = {
        authors: [],
        books: [],
    };

    getAuthors = (arr) => {
        if (arr) {
            return arr.join('\r\n');
        } else {
            return 'Unknown author';
        }
    };

    render() {

        return (
            <li>
                {book.authors && book.authors.map((author) => (
                    <span className='book-authors'>{author}</span>
                ))}
            </li>
        );
    }
}

export default Book;

最佳答案

getAuthors是一个类属性。这是一个拟议的演变,您可以使用babel-plugin-transform-class-properties使Babel对其进行转译

关于reactjs - 带有React和Babel语法错误的Brunch.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47198446/

相关文章:

javascript - 如何将 setTimeout 与 Loader 一起使用

javascript - 更新时的 React Redux 调度会导致递归错误

angular - 在 typescript 中传播语法

reactjs - 理解 typescript 中的 `as unknown as`

javascript - ES6 应用程序中第三方 javascript Uncaught ReferenceError

javascript - JS (ES6) : Reduce array based on object attribute

mysql - 为什么这个 ALTER TABLE 查询会抛出语法错误?

c++ - 错误: Invalid conversion from 'int' to 'const char*'

sql - 从SQL Server中的表创建表

javascript - Redux 不更新组件