【问题标题】:React mobx does not inject stores properlyReact mobx 没有正确注入存储
【发布时间】:2017-07-17 16:15:33
【问题描述】:

我已经实现了这个,但是商店没有值(全部未定义):

这是商店:

export default class AppState {

    // Is authenticated
    @observable authenticated;

    @action get authenticated() {
        return this.authenticated;
    }

    doSomethingWithNoDecorator() {
      return this.authenticated;
    }
 }

这是 index.js:

const stores = {
    AppState
};

const renderApp = Component => {
    render(
        <AppContainer>

            <Provider { ...stores }>
                <Router>
                    // Routes
                </Router>
            </Provider>
        </AppContainer>,
        document.getElementById("root")
    );
};

这是组件:

@inject("AppState")
@observer
export default class SidebarListItem extends Component {

    constructor(props) {
        super(props)
        this.store = this.props.AppState;
    }

    doSomething() {
        this.store.authenticated();
        this.store.doSomethingWithNoDecorator();
        this.store.authenticated;        
    }
}

商店不为空...我可以看到功能。但我无法获取任何字段或调用任何方法。

我做错了什么?

问候, 伊多布

【问题讨论】:

    标签: mobx-react


    【解决方案1】:

    您需要初始化您的商店:

    const stores = { AppState: new AppState() }
    

    顺便说一句,@actions 不能应用于 getter。

    【讨论】:

      猜你喜欢
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多