(注意:糞記事です)
mobs-state-treeは便利なのですが、よく循環依存を作りがちです。
こういうのでコンパイルが死んでいませんか?
import { types, getParentOfType } from "mobx-state-tree";
const Parent = types.model("Parent", {
child: types.late(() => Child)
}).actions(self => {
return {}
})
const Child = types.model("Child", {}).views(self => {
return {
get parent() {
// 下記の評価結果は any になり、そしてエラーに!
return getParentOfType(self, Parent)
}
}
})
でも下記はOKだったりします
const Parent = types.model("Parent", {}).views(self => {
return {
get child() {
return Child.create({})
}
}
})
const Child = types.model("Child", {}).views(self => {
return {
get parent() {
return getParentOfType(self, Parent)
}
}
})
これもOK
import { types, getParentOfType } from "mobx-state-tree";
const Parent = types.model("Parent", {
child: types.late(() => Child)
})
const Child = types.model("Child", {})
.actions(self => {
const parent = () => getParentOfType(self, Parent)
return {
doSomething() {
parent().child
}
}
})