意外的令牌PropTypes

时间:2016-10-15 19:18:37

标签: javascript reactjs ecmascript-6 babeljs

我正在使用"react": "^15.3.2",我希望对PropTypes的支持来自Box。

但是,以下每个陈述都以错误结束,我正着手解决此问题。

import React, { Component, PropTypes } from 'react';

...

static defaultProps = {
  evalScripts: 'never',
  callback: () => {}
}

static propTypes = {
  path: PropTypes.string.isRequired,
  className: PropTypes.string,
  evalScripts: PropTypes.oneOf(['always', 'once', 'never']),
  fallbackPath: PropTypes.string,
  callback: PropTypes.func
}

错误:

Error thrown for request: /js/application.js
SyntaxError: /home/user/react-project/client/scripts/reactsvg.js: Unexpected token (6:22)
export default class ReactSVG extends Component {

  static defaultProps = {
    evalScripts: 'never',
    callback: () => {}
  }
    at Parser.pp.raise (/home/user/react-project/node_modules/babylon/lib/parser/location.js:24:13)
    at Parser.pp.unexpected (/home/user/react-project/node_modules/babylon/lib/parser/util.js:82:8)
    at Parser.pp.parseClassProperty (/home/user/react-project/node_modules/babylon/lib/parser/statement.js:624:61)
    at Parser.parseClassProperty (/home/user/react-project/node_modules/babylon/lib/plugins/flow.js:836:20)
    at Parser.pp.parseClass (/home/user/react-project/node_modules/babylon/lib/parser/statement.js:567:32)
    at Parser.pp.parseExprAtom (/home/user/react-project/node_modules/babylon/lib/parser/expression.js:405:19)
    at Parser.parseExprAtom (/home/user/react-project/node_modules/babylon/lib/plugins/jsx/index.js:412:22)
    at Parser.pp.parseExprSubscripts (/home/user/react-project/node_modules/babylon/lib/parser/expression.js:236:19)
    at Parser.pp.parseMaybeUnary (/home/user/react-project/node_modules/babylon/lib/parser/expression.js:217:19)
    at Parser.pp.parseExprOps (/home/user/react-project/node_modules/babylon/lib/parser/expression.js:163:19)
    at Parser.pp.parseMaybeConditional (/home/user/react-project/node_modules/babylon/lib/parser/expression.js:145:19)
    at Parser.pp.parseMaybeAssign (/home/user/react-project/node_modules/babylon/lib/parser/expression.js:112:19)
    at Parser.pp.parseExport (/home/user/react-project/node_modules/babylon/lib/parser/statement.js:682:21)
    at Parser.parseExport (/home/user/react-project/node_modules/babylon/lib/plugins/flow.js:713:20)
    at Parser.pp.parseStatement (/home/user/react-project/node_modules/babylon/lib/parser/statement.js:115:90)
    at Parser.parseStatement (/home/user/react-project/node_modules/babylon/lib/plugins/flow.js:655:22)
    at Parser.pp.parseTopLevel (/home/user/react-project/node_modules/babylon/lib/parser/statement.js:30:21)
    at Parser.parse (/home/user/react-project/node_modules/babylon/lib/parser/index.js:70:17)
    at Object.parse (/home/user/react-project/node_modules/babylon/lib/index.js:45:50)
    at Object.exports.default (/home/user/react-project/node_modules/babel-core/lib/helpers/parse.js:36:18)
    at File.parse (/home/user/react-project/node_modules/babel-core/lib/transformation/file/index.js:574:40)
    at File.parseCode (/home/user/react-project/node_modules/babel-core/lib/transformation/file/index.js:691:20)
GET /js/application.js 500 2296.944 ms

1 个答案:

答案 0 :(得分:1)

您收到语法错误。我真的不知道为什么。但这是我的赌注。

static defaultProps = { ... };

导致此错误,因为这是ES7功能。也许您可以使用babel-preset-es2017预设来解决此问题。或者只是使用

ReactSVG.defaultProps = { ... };

在语义上是相同的。