【发布时间】:2019-10-24 07:41:10
【问题描述】:
我想订阅一个返回简单对象的计算属性。
代码沙盒:https://codesandbox.io/s/epic-noether-g8g0x
我做了一个简单的例子来说明问题所在:
const zeroSize = {
width: 0,
height: 0
};
class Item {
@observable size = zeroSize;
}
class Collection {
@observable childrens: Item[] = [];
@computed get size() {
return this.childrens.length > 0 && this.childrens[0] ? this.childrens[0].size : zeroSize;
}
constructor() {
reaction(() => this.size, size => {
// Expected { width: 0, height: 1000 }
// Actual { width: 0, height: 0 }
console.log('-----size=', toJS(size));
});
}
}
const item1 = new Item;
const collection1 = new Collection;
collection1.childrens.push(item1);
item1.size.height = 1000;
【问题讨论】:
标签: reactjs mobx mobx-react