mobx +反应意外的令牌

时间:2016-12-14 09:03:52

标签: reactjs mobx mobx-react

所以我在https://facebook.github.io/react/blog/2016/07/22/create-apps-with-no-configuration.html

创建了一个没有配置的应用程序

我安装了mobx和mobx-react,但在@ symb之前仍显示意外令牌的错误。

我是否需要添加其他内容,或者我当前的配置是错误的?:(

的package.json

"devDependencies": {
"react-scripts": "0.8.4",
"babel-core": "^6.7.6",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0"
},
"dependencies": {
"autobind-decorator": "^1.3.4",
"classnames": "^2.2.5",
"lodash": "^4.15.0",
"mobx": "^2.5.1",
"mobx-react": "^3.5.5",
"react": "^15.3.1",
"react-dom": "^15.3.1",
"validator": "^5.6.0"
 },

.babelrc

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

代码

import React, { Component } from 'react';
import { action, observable } from 'mobx'
import {observer} from 'mobx-react';


class App {
    @observer cake = [];
}

export default new App();

3 个答案:

答案 0 :(得分:14)

create-react-app不支持装饰器(@)。您可以eject创建 - react-app自行添加,从头开始设置自己的环境,或者使用mobx-react-boilerplate之类的东西作为起点。

我亲自使用react-app-rewiredmobx extension取得了巨大成功。

答案 1 :(得分:7)

运行npm run eject后您缺少包(因为create-react-app不支持装饰器)。

运行npm install --save-dev babel-plugin-transform-decorators-legacy babel-plugin-transform-class-properties

然后将以下Babel配置添加到package.json

"babel": {
  "plugins": [
    "transform-decorators-legacy"
  ],
  "presets": [
    "react-app"
  ]
},

答案 2 :(得分:4)

您可以使用不使用装饰器(herehere)的语法。

以下是使用您提供的App类代码的示例:

import React, { Component } from 'react';
import { action, extendObservable } from 'mobx'
import {observer} from 'mobx-react';


class App {
  constructor() {
    extendObservable(this, {
      cake = [],
    }); 
  }
}

export default new App();