没有装饰器的reactjs mobx无法正常工作
reactjs mobx without decorators not working
我正在尝试将 mobx
与 react
合并。由于我使用 create-react-app
生成了我的应用程序,因此我无法使用 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(…)
控制台中没有任何显示。
我做错了什么?
请使用import {observer} from 'mobx-react';
N.B。请注意,装饰器可以通过使用 custom-react-scripts, as explained here)
与 create-react-app 一起使用
我正在尝试将 mobx
与 react
合并。由于我使用 create-react-app
生成了我的应用程序,因此我无法使用 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(…)
控制台中没有任何显示。
我做错了什么?
请使用import {observer} from 'mobx-react';
N.B。请注意,装饰器可以通过使用 custom-react-scripts, as explained here)
与 create-react-app 一起使用