2016-11-08 90 views
3

我想將mobxreact合併。由於我使用create-react-app衍生出我的應用程序,因此我無法使用mobx提供的裝飾器。reactjs mobx沒有修飾器無法正常工作

既然我們可以用mobx無裝飾按本文件:https://mobxjs.github.io/mobx/best/decorators.html

下面是我創建了一個組件:

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

export const TestComponent = observer(class TestComponent extends Component { 

    render() { 
    return <div>Just a test component!</div> 
    } 

}); 

這裏的上述部件的簡單通話:

import React, { Component } from 'react'; 

import './App.css'; 

import Auth from './Auth' 
import { TestComponent } from './Test' 

import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'; 

import AppBar from 'material-ui/AppBar'; 
import authStore from './stores/Store' 

class App extends Component { 

    constructor(props) { 
     super(props); 
     this.state = { 
     } 
    } 

    render() { 
    return (
     <div className="app"> 
      <MuiThemeProvider> 
       <div> 
        <TestComponent store={authStore} /> 
       </div> 
      </MuiThemeProvider> 
     </div> 

    ); 
    } 
} 

export default App; 

現在,當我運行上面的組件,我得到錯誤:Uncaught TypeError: (0 , _mobxReact2.default) is not a function(…)沒有得到控制檯中的顯示。

我在這裏做錯了什麼?

回答

2

請使用import {observer} from 'mobx-react';

N.B.請注意,裝飾者可以使用create-react-app使用custom-react-scripts,如解釋here

+0

謝謝。我錯過了這個基本的東西。 在附註上,我可以使用自定義反應腳本與我已經在工作的項目嗎?我不想創建一個新的。 – Jazib

+0

afaik是的,這是可能的 – mweststrate

+0

你能給我一個指針,我怎麼能做到這一點? – Jazib