Javascript 在Mobx状态树中进行更改后,如何让模型/状态侦听?

Javascript 在Mobx状态树中进行更改后,如何让模型/状态侦听?,javascript,reactjs,mobx-react,mobx-state-tree,Javascript,Reactjs,Mobx React,Mobx State Tree,我有两个模型/商店,每个都涉及一个对象。Store1填充并接收childId后,Store2将填充。当Store1被填充时,我将如何让Store2收听 仓库1 import {action} from 'mobx' import axios from "axios"; import {types} from "mobx-state-tree"; const Store1 = types.model('Store1', { id: types.number, name: type

我有两个模型/商店,每个都涉及一个对象。Store1填充并接收childId后,Store2将填充。当Store1被填充时,我将如何让Store2收听

仓库1

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;
Store2(在Store1从API调用获取childId之前无法填充)


如果
Store2
依赖于
Store1
,则最好在
Store2
实例中引用它。然后,您只需添加一个观察
store1
实例属性的视图,一旦观察到的属性更新,该视图将自动重新计算自身

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

const Store2 = types.model('Store2', {
    id2: types.number,
    name2: types.string,
    description2: types.string
}).actions(self => ({
    onChange(name, value) {
        self[name] = value;
    },
    //should receive id from store1 childId
    getInfo(childId) {
        //api call that gets info and returns data
        self.id2= data.id
        self.name2 = data.name
        self.description2 = data.description
    },
}));

const store2 = Store2.create({
    id2: 0,
    name2: '',
    description2: ''
});
export default store2;