我的 MobX 自动运行函数为这里的两个值注销 undefined
。此外,在我的 import user from '../../stores/UserStore
组件中,我使用 {user.userName}
的实例都没有显示任何内容。
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
})
最佳答案
如果你想在 create-react-app 中使用装饰器,你需要弹出应用程序。 “运行 npm run eject 会复制所有配置文件和传递依赖项。” https://github.com/facebookincubator/create-react-app我还发现你需要像这样首先放置 transform-decorators-legacy 插件:
plugins: ['transform-decorators-legacy','transform-class-properties']
完成所有这些之后。以下将起作用并已经过测试。
@observer class Counter extends React.Component {
@observable count = 0
}
关于javascript - react /MobX : Store values are undefined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39481614/