【问题标题】:Access stores from class using mobX and react Context使用 mobX 从类访问存储并响应上下文
【发布时间】:2019-06-13 06:21:11
【问题描述】:

我在 mobx/mobx-react-lite 和 react 钩子上有点挣扎。

我想从一个班级更新我的一个商店中的一个属性,但不知何故我无法让它工作。以下是我的商店如何组合的一些示例,以及我想从中调用我的商店的组件和类。我正在使用 react 中的 Context 来获取我的钩子组件中的商店,并且效果很好。

// FooStore

import { observable, action } from "mobx";
import ExampleClass from "app/services/exampleClass";

export class FooStore {
    @observable
    public foo: string = "";

    @action
    public doSomething() {
        this.foo = ExampleClass.doSomething()
    }
}

export default FooStore;

// 酒吧商店

import { observable, action } from "mobx";

export class BarStore {
    @observable
    public bar: number = 0;

    @action
    public setBar(value: number) {
        this.bar
    }
}

export default BarStore;

//Store(将store合二为一,用createContext()导出)

import { FooStore } from "./FooStore";
import { BarStore } from "./BarStore";
import { createContext } from "react";

class Store {
    public fooStore: FooStore;
    public barStore: BarStore;
    constructor(){
        this.fooStore = new FooStore();
        this.barStore = new BarStore();
    }
}

const stores = new Store()

export default createContext(stores);

这是我希望能够调用我的 barStore 的类。 (注意,不是组件类)

//示例类

export default class ExampleClass {
    public static doSomething(): string {
        // ...

        // Call BarStore.setBar(1000)

        return "Some string"
    }
}

任何人都可以将我推向正确的方向吗?

【问题讨论】:

    标签: reactjs typescript mobx mobx-react


    【解决方案1】:

    上下文是一个 React 概念。通过 Context 导出你的商店是不好的。 (可能您应该需要在另一个环境中使用它!)您应该导出存储本身并通过上下文将其包装在您的最高级别组件中。

    //您的店铺:

    import { FooStore } from "./FooStore";
    import { BarStore } from "./BarStore";
    
    class Store {
        public fooStore: FooStore;
        public barStore: BarStore;
        constructor(){
            this.fooStore = new FooStore();
            this.barStore = new BarStore();
        }
    }
    
    const stores = new Store()
    
    export default stores;
    

    //App.js ...

    import store from './yourStore';
    import { createContext } from "react";
    
    const GlobalStore = createContext(store);
    
    export default () => {
        <GlobalStore.Provider>
           <Main />
        </GlobalStore.Provider>
    }
    

    //其他任何js文件

    import store from './yourStore';
    
    export default class ExampleClass {
        public static doSomething(): string {
            // ...
    
            store.BarStore.setBar(1000)
    
            return "Some string"
        }
    }
    

    【讨论】:

    • 嘿,Ehssan!谢谢你的帮助。我已将 添加到我的 app.ts,但它需要一个值,所以我最终得到了 。但是如何通过在我的组件中使用 useContext 从上下文中获取商店?还是您建议像 const store = store (from ./yourStore) 一样导入它
    • @Englund0110;您可以创建一个模块并在其中调用 React.createContext 。然后导出输出值。在任何你想要的地方使用这个模块,并通过 useContext 将其传递下去。
    • 在这种情况下,Store 不是一个单例,因此在 ExampleClass 中根本不使用 Context 吗?我相信正确的方法是设置ExampleClass.contextType = GlobalContext,然后在课堂上使用this.context.fooStore,但我只是想把我的头放在整个上下文的事情上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多