在Mobx状态树中进行更改后,如何获取模型/状态以进行监听?

时间:2019-05-08 02:08:18

标签: javascript reactjs mobx-react mobx-state-tree

我有两个模型/存储,每个模型/存储都引用一个对象。在填充Store1并接收childId之后,填充Store2。当填充Store1时,Store2将如何收听?

Store1

for item in upList:
    if item in myTable:
        cell = 'B' + str(myTable.index(item))
        sht.range(cell).value = "Event"

Store2(直到Store1从API调用中获得childId之前,该字段无法填充

import {action} from 'mobx'
import axios from "axios";
import {types} from "mobx-state-tree";

const Store1 = types.model('Store1', {
    id: types.number,
    name: types.string,
    description: types.string,
    childId: types.number
}).actions(self => ({
    onChange(name, value) {
        self[name] = value;
    },
    getInfo(id) {
        //API call that gets uses id and returns data
        self.id = data.id
        self.name = data.name
        self.description = data.description
        self.childId = data.childId
    },
}));

const store1 = Store1.create({
    id: 0,
    name: '',
    description: '',
    childId: 0
});
export default store1;

1 个答案:

答案 0 :(得分:1)

如果Store2依赖于Store1,则最好在store2实例中对其进行引用。然后,您可以简单地添加一个观察store1实例的属性的视图,一旦观察到的属性更新,它将自动重新计算自身。