2016-09-14 10 views
3

私のMobX自動実行機能は、両方の値に対してundefinedをログアウトします。また、私のコンポーネントであるimport user from '../../stores/UserStoreでは、私が{user.userName}を使用するインスタンスは、まったく何も表示しません。React/MobX:ストア値は未定義ですか?

import { observable, autorun } from 'mobx' 

class UserStore { 
    @observable userName = "tommy"; 
    @observable arr = [0,1] 
} 

const user = window.user = new UserStore 

export default user; 


// user.userName = "timmy"; // works, but not observable. 

autorun(() => { 
    console.log(user.userName) // undefined 
    console.log(user.arr) // undefined 
}) 
+0

あなたは '新しいUserStore()'を意味しましたか? – azium

+0

[それは私のために働く](http://jsbin.com/jumipucalo/edit?js,console)。コンソールで 'user'のフィールドを変更してみてください。多分、あなたは[この問題](http://stackoverflow.com/questions/39381706/i-cant-figure-out-why-the-input-is-not-updating-using-mobx)を実行していますか? – Tholle

+0

私は問題が 'create-react-app'の私の使い方と関係していることに気付きました。私は観察者/観察可能な機能を動作させることができましたが、デコレータは使用できませんでした。 'observer(デフォルトのクラスをエクスポートするMyComponentはComponentを拡張します。... と class MyStore {myValue = observable( 'some value here')}' –

答えて

2

デコレータをcreate-react-appで使用する場合は、アプリケーションを取り出す必要があります。

plugins: ['transform-decorators-legacy','transform-class-properties'] 

あなたはすべてこれを実行した後:「実行NPMの実行すべての設定ファイルと推移的な依存コピーを取り出しhttps://github.com/facebookincubator/create-react-appはまた、私はあなたがそうのように最初のプラグイン変換-デコレータレガシーを配置する必要がありました。以下は動作し、テストされています。

@observer class Counter extends React.Component { 
    @observable count = 0 
} 
+1

アンサー付きの回答。最初にデコレータプラグインを置くのに感謝します。 –