【问题标题】:reactjs mobx without decorators not working没有装饰器的reactjs mobx不起作用
【发布时间】:2017-03-21 19:14:18
【问题描述】:

我正在尝试将mobxreact 合并。由于我使用 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(…) 控制台中没有显示任何内容。

我在这里做错了什么?

【问题讨论】:

    标签: reactjs decorator mobx create-react-app mobx-react


    【解决方案1】:

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

    注意请注意,装饰器可以通过使用 custom-react-scripts 与 create-react-app 一起使用,如 here 所述)

    【讨论】:

    • 谢谢。我错过了这个基本的东西。附带说明一下,我可以在我已经工作的项目中使用 custom-react-scripts 吗?我不想创建一个新的。
    • afaik 是的,这是可能的
    • 最好问问上面博客的作者,但我希望 npm 安装包和创建配置文件应该可以解决问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2020-07-06
    • 2017-09-28
    • 1970-01-01
    • 2018-07-11
    • 2019-03-09
    • 2016-03-14
    相关资源
    最近更新 更多