每当我尝试从mobx-react
导入观察者时,应用程序崩溃时会出现500错误代码。
我安装/配置了mobx错了吗?
1- npm i mobx mobx-react --save
2- npm i babel-plugin-transform-decorators-legacy
babel-preset-react-native-stage-0 --save-dev
3-在根文件夹中.babelrc:
`{'presets': ['react-native'],'plugins': ['transform-decorators-legacy']}`
4- Package.json:
"dependencies": {
"react": "^15.4.2",
"react-native": "^0.40.0",
"mobx": "^3.0.2",
"mobx-react": "^4.1.0"
}
Store.js:
import { observable, computed } from 'mobx'
class GetFoo {
@observable Foo = "bar"
root() {
return this.Foo
}
}
let store = new GetFoo()
export default store
应用组件:
import React, { Component } from 'react'
import {
Text,
View
} from 'react-native'
import NetProvider from './config/netInfo'
import { observer } from 'mobx-react'
@observer
export default class AppRoot extends Component {
constructor(props) {
super(props);
}
render() {
console.log(this.props)
return(
<NetProvider>
<View>
<Text>Hi</Text>
</View>
</NetProvider>
)
}
}
答案 0 :(得分:1)
您需要将mobx-react/native
用于React Native:
import { observer } from 'mobx-react/native'