我在用mobx / mobx-react-lite挣扎并做出反应。
我想从一个类中更新一个商店中的属性,但是以某种方式我无法使它工作。以下是一些示例,这些示例说明了如何合并我的商店,以及我想从中调用我的商店的组件和类。我正在使用Context从react获取要存储在我的钩子组件中,并且效果很好。
// 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;
// BarStore
import { observable, action } from "mobx";
export class BarStore {
@observable
public bar: number = 0;
@action
public setBar(value: number) {
this.bar
}
}
export default BarStore;
// 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的类。 (注意,不是组件类)
// ExampleClass
export default class ExampleClass {
public static doSomething(): string {
// ...
// Call BarStore.setBar(1000)
return "Some string"
}
}
有人可以为此推我正确的方向吗?
答案 0 :(得分:0)
上下文是一个React概念。通过Context导出商店是不好的。 (可能是您应该需要在其他环境中使用它!)您应该导出存储本身,并通过上下文将其包装在最高级别的组件中。
//您的商店:
DELETE FROM STATE;
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('AL','Alabama ');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('AK','Alaska');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('AZ','Arizona');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('AR','Arkansas');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('CA','California');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('CO','Colorado');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('CT','Connecticut');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('DE','Delaware');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('FL','Florida');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('GA','Georgia');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('HI','Hawaii');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('ID','Idaho');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('IL','Illinois');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('IN','Indiana');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('IA','Iowa');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('KS','Kansas');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('KY','Kentucky');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('LA','Louisiana');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('ME','Maine');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('MD','Maryland');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('MA','Massachusetts');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('MI','Michigan');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('MN','Minnesota');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('MS','Mississippi');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('MO','Missouri');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('MT','Montana');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('NE','Nebraska');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('NV','Nevada');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('NH','New Hampshire');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('NJ','New Jersey');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('NM','New Mexico');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('NY','New York');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('NC','North Carolina');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('ND','North Dakota');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('OH','Ohio');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('OK','Oklahoma');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('OR','Oregon');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('PA','Pennsylvania');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('RI','Rhode Island');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('SC','South Carolina');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('SD','South Dakota');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('TN','Tennessee');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('TX','Texas');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('UT','Utah');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('VT','Vermont');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('VA','Virginia');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('WA','Washington');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('WV','West Virginia');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('WI','Wisconsin');
INSERT INTO STATE (STATE_CD,STATE_NAME) VALUES ('WY','Wyoming');
COMMIT;
// App.js ...
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;
//其他任何js文件
import store from './yourStore';
import { createContext } from "react";
const GlobalStore = createContext(store);
export default () => {
<GlobalStore.Provider>
<Main />
</GlobalStore.Provider>
}